Solved

Google custom search API does not return result - help required

Posted on 2012-03-21
4
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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