Solved

Google custom search API does not return result - help required

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

770 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