Solved

Google custom search API does not return result - help required

Posted on 2012-03-21
4
416 Views
Last Modified: 2012-04-12
Following is the code snippet I use which would return zero search results for lengthy search key.

$search_key = "site:play.google.com ".$search_string;
$url = "https://www.googleapis.com/customsearch/v1?key=AIzaSyDgnGNNXCqmdp2Hh2V_CtUPGTb2ulzo0GM&cx=013380388901817775724:zb4u2bzxno4&q=".str_replace(" ", "+", $search_key);

Open in new window


It works well when the search key is shorter. Butyields zero results with longer search keys:

eg: Seach key site:play.google.com Computer and Mathematical Occupations Information Technology and even lengthier ones yielded no results. But they would return results when I do normal google search.

What is the solution I have such that I can use longer search keys and yield the same search results as the search key would have returned with a normal google search?
0
Comment
Question by:Sreejith22
  • 2
4 Comments
 
LVL 20

Expert Comment

by:virmaior
ID: 37752470
Did google tell you to replace the spaces with +?

I don't think that's the right way to encode it...

instead of the str_replace you have, use urlencode()

 http://php.net/manual/en/function.urlencode.php

the correct encoded version is %20.

I think what's happening is that it is interpreting those as individually required terms.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37752915
Both of these requests seem to work perfectly.
See http://www.laprbass.com/RAY_temp_sree.php
<?php // RAY_temp_sree.php
error_reporting(E_ALL);
echo "<pre>";

// SOME TEST DATA
$search_str = 'Computer and Mathematical Occupations Information Technology';

// CONCATENATE TO THE SEARCH AREA NAME
$search_key
= "site:play.google.com"
. ' '
. $search_str
;

// CREATE THE GET REQUEST
$url
= "https://www.googleapis.com/customsearch/v1?key=AIzaSyDgnGNNXCqmdp2Hh2V_CtUPGTb2ulzo0GM&cx=013380388901817775724:zb4u2bzxno4&q="
. urlencode($search_key)
;

// CALL THE RESOURCE AND SHOW THE RESULTS
$res = file_get_contents($url);
$obj = json_decode($res);
var_dump($obj);



// CREATE THE GET REQUEST WITH PLUS SIGNS IN IT
sleep(1);
echo PHP_EOL . '********************************************************************************************************';
$search_str = 'Computer+and+Mathematical+Occupations+Information+Technology';
$search_key
= "site:play.google.com"
. '+'
. $search_str
;
$url
= "https://www.googleapis.com/customsearch/v1?key=AIzaSyDgnGNNXCqmdp2Hh2V_CtUPGTb2ulzo0GM&cx=013380388901817775724:zb4u2bzxno4&q="
. $search_key
;

// CALL THE RESOURCE AND SHOW THE RESULTS
$res = file_get_contents($url);
$obj = json_decode($res);
var_dump($obj);

Open in new window

0
 

Author Comment

by:Sreejith22
ID: 37770772
will check and update.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37771220
OK.  Just click the link at http://www.laprbass.com/RAY_temp_sree.php to see the script in action.  HTH, ~Ray
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now