Solved

Google custom search API does not return result - help required

Posted on 2012-03-21
4
426 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 109

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 109

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …

821 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