tags:

views:

539

answers:

3

I am having a problem doing a get on googles search url from php. Here's the code I have:

<?php
    $handle = fopen("http://www.google.com/complete/search?hl=en&amp;js=true&amp;qu=" .
     $_GET["qu"], "r");
    while (!feof($handle)) {
     $text = fgets($handle);
     echo $text;
    }
    fclose($handle);
?>

Here's the error I get:

PHP Warning: fopen(http://www.google.com/complete/search?hl=en&amp;js=true&amp;qu=cat): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Inetpub\test\google.php on line 3 PHP Fatal error: Maximum execution time of 60 seconds exceeded in C:\Inetpub\test\google.php on line 3

I'm using fiddler, and doing a request on the url itself works fine, but for some reason the php does not. Anyone have any idea why?

update: Here is my javascript:

function getSuggest(keyEvent) {
  keyEvent = (keyEvent) ? keyEvent : window.event;
  input = (keyEvent.target) ? keyEvent.target : keyEvent.srcElement;

  if (keyEvent.type == "keyup") {
    if (input.value) {
      getSearchData("google.php?qu=" + input.value);
    } else {
 var target = document.getElementById("targetDiv");
 target.innerHTML = "<div></div>";
    }
  }
}

function getSearchData(dataSource) {
  if (XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", dataSource);
 XMLHttpRequestObject.onreadystatechange = function() {
   if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
     eval(XMLHttpRequestObject.responseText);
        }
 }
    XMLHttpRequestObject.send(null);
  }
}

function sendRPCDone(unusedVariable, searchTerm, arrayTerm, arrayResults, ususedArray) {
  var data = "<table>";
  var loopIndex;

  if (arrayResults.length != 0) {
    for (var loopIndex = 0; loopIndex < arrayResults.length; loopIndex++) {
 data += "<tr><td>" + "<a href='http://www.google.com/search?q=" +
   arrayTerm[loopIndex] + "'>" + arrayTerm[loopIndex] + '</a></td><td>' +
   arrayResults[loopIndex] + "</td></tr>";
    }
  }

  data += "</table>";
  var targetDiv = document.getElementById("targetDiv");
  targetDiv.innerHTML = data;
}

And here is my html:

<div id="googleSearch">
  Search For <input id="textField" type="text" name="textField" onkeyup="getSuggest(event)" />
  <div id="targetDiv4"></div>
</div>
+3  A: 

That looks like a timeout. It could be that the server you are trying to communicate with discriminates requests based on USER_AGENT.

karim79
TahoeWolverine
The first would generate a request to Google _from PHP_, not from your browser, so they will have different user agents.
Lucas Oman
This could be it also... I believe Fiddler spoofs IE's user agent, so google thinks a browser is visiting it, so it will allow it. While the PHP script will have a different user agent which Google may not allow (because it is a script vs. a browser). I'm not 100% on any of that tho.
Polaris878
<< I believe Fiddler spoofs IE's user agent>> No, not unless you tell it to.
EricLaw -MSFT-
+1  A: 

Have you verified the php.ini allow_url_fopen is ON ? Plus the default_socket_timeout ?

; Whether to allow the treatment of URLs (like http:// or ftp://) as files. allow_url_fopen = On

; Whether to allow include/require to open URLs (like http:// or ftp://) as files. allow_url_include = Off

; Default timeout for socket based streams (seconds) default_socket_timeout = 60

I agree it looks like a timeout.

Are you working with PHP 5, if so, you could try file_get_contents().

mere-teresa
I have not verified or tried any of that. How do I do so?
TahoeWolverine
basicely a simple file with the phpinfo(); instruction let you know about this settings otherwise, ask your sysadmin
mere-teresa
I think if that that were not enabled we wouldn't get as far as a timeout (not entirely sure though)
karim79
yep all settings are as expected.
TahoeWolverine
and file_get_contents() is no different.
TahoeWolverine
+1  A: 

Edit:

If it turns out you are having user agent string problems, you can set the user-agent php uses by creating and running a file with the following code in it: <?php ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1) Gecko/20090615 Firefox/3.5'); ?>

I think you'll have to restart IIS after setting this, but not 100% on that.

Note: this is a random user agent string I pulled, there are many out there, you can set it to pretty much anything you want. There are many more at: http://www.useragentstring.com/


To check if allow_url_fopen is on, do this:

  1. Create a php file on your server, name it whatever you desire.
  2. Put this into your file <?php phpinfo(); ?>
  3. Execute the script on your server through a web browser or fiddler if you are using that
  4. check for all necessary settings.

Let us know what it is, then we can walk you through setting it to what you need.

Polaris878
Thanks for the clear answer. Haven't found the problem yet though.
TahoeWolverine