PHP fopen无法响应 [英] PHP fopen fails in response

查看:371
本文介绍了PHP fopen无法响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从php获取googles搜索网址时遇到问题。以下是我的代码:

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

这是我得到的错误:


PHP警告:
fopen( http://www.google.com/complete/search?hl=zh-CN&js=true&qu=cat ):
无法打开流:连接
尝试失败,因为连接的
方在
时间段后没有正确响应,或者建立的
连接失败,因为连接的
主机未能响应。 in
C:\ Inetpub \test\google.php on line 3
PHP致命错误:最大执行时间

中超过60秒的时间C:\ Inetpub \test\google.php on line 3


我使用fiddler,并且在url本身上执行请求时工作正常,但由于某种原因,PHP不会。任何人有任何想法为什么?



更新:这是我的javascript:

  function getSuggest(keyEvent){
keyEvent =(keyEvent)? keyEvent:window.event;
input =(keyEvent.target)? keyEvent.target:keyEvent.srcElement; $(b)
$ b if(keyEvent.type ==keyup){
if(input.value){
getSearchData(google.php?qu =+ input.value) ;
} else {
var target = document.getElementById(targetDiv);
target.innerHTML =< div>< / div>;



$ b函数getSearchData(dataSource){
if(XMLHttpRequestObject){
XMLHttpRequestObject.open(GET,数据源);
XMLHttpRequestObject.onreadystatechange = function(){
if(XMLHttpRequestObject.readyState == 4&& XMLHttpRequestObject.status == 200){
eval(XMLHttpRequestObject.responseText);
}
}
XMLHttpRequestObject.send(null);



函数sendRPCDone(unusedVariable,searchTerm,arrayTerm,arrayResults,ususedArray){
var data =< table>;
var loopIndex; $(变量循环索引= 0;循环索引< arrayResults.length;循环索引++){
数据+ =< tr>

if(arrayResults.length!= 0) ;< 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;
}

这里是我的html:

 < div id =googleSearch> 
搜寻< input id =textFieldtype =textname =textFieldonkeyup =getSuggest(event)/>
< div id =targetDiv4>< / div>
< / div>


解决方案

编辑



如果事实证明你有用户代理字符串问题,你可以通过创建并运行一个文件来设置用户代理php的使用:
<?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'); ?>



我认为您必须在设置后重新启动IIS,但不是100%。

b
$ b

注意:这是一个随机的用户代理字符串,我拉了很多,你可以将它设置为几乎任何你想要的。还有更多: http://www.useragentstring.com/






要检查allow_url_fopen是否打开,请执行以下操作:


  1. 在您的服务器上创建一个php文件,将其命名为任何您想要的名称。
  2. 放入您的文件<?php phpinfo(); ?>

  3. 检查您的服务器上的脚本是否通过网络浏览器或提琴手执行对于所有必要的设置。

让我们知道它是什么,然后我们可以引导您将其设置为您需要的。 p>

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&js=true&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&js=true&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>

解决方案

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.

这篇关于PHP fopen无法响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆