?
Solved

Google custom search API does not return result - help required

Posted on 2012-03-21
4
Medium Priority
?
434 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

719 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