php 使用 cURL 获取错误代码 28
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6678487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting error code 28 with cURL
提问by runey71
THIS HAS BEEN SOLVED - SEE ANSWER AT THE END OF THIS POST
这已经解决了 - 请参阅本文末尾的答案
I am trying to retrieve data from a remote server using PHP / cURL
我正在尝试使用 PHP / cURL 从远程服务器检索数据
If I put the following URL into a browser the data comes back correctly.
如果我将以下 URL 放入浏览器,数据将正确返回。
However when I try to access if with PHP / cURL it just times out (error code 28).
但是,当我尝试使用 PHP / cURL 访问 if 时,它只是超时(错误代码 28)。
$json = curl($jsonurl, $realm['intRealmID'], $realm['strRealmServer']);
function curl($url, $realm, $realmServer){
$header = array();
$header[] = 'Host: realm'.strval($realm).'.'.$realmServer.'.castle.wonderhill.com';
$header[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$header[] = 'Accept-Language: en-us,en;q=0.5';
$header[] = 'Accept-Encoding: gzip,deflate';
$header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7';
$header[] = 'Connection: keep-alive';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
return curl_exec($ch);
curl_close($ch);
}
}
Anybody have any ideas why it works from the browser but not via cURL? Thanks
任何人都知道为什么它可以在浏览器中运行而不是通过 cURL 运行?谢谢
ADDITIONAL INFO Whilst cURL isn't working for the URL above. For the URL below it works just fine. The only difference is the server the data is being requested from. The data itself and POST is identical.
附加信息 虽然 cURL 不适用于上面的 URL。对于下面的 URL,它工作得很好。唯一的区别是请求数据的服务器。数据本身和 POST 是相同的。
ANSWER
回答
Finally back to this and found that the referrer was the problem. The server was expecting to see no referrer in the request header. When it did the request was blocked. That behaviour probably was not consistent across all servers at the time but it is now. Removing the referrer from the request header and leaving everything else the same now works.
终于回到这个,发现是referrer的问题。服务器期望在请求标头中看不到引用者。当它完成时,请求被阻止。当时这种行为可能在所有服务器上都不一致,但现在是。从请求标头中删除引用者并使其他所有内容保持不变现在可以工作。
回答by Ryan
The biggest difference between your cURL function and requesting the information directly is the CURLOPT_HEADER
property, I would first try removing this from the code.
您的 cURL 函数和直接请求信息之间的最大区别是CURLOPT_HEADER
属性,我会首先尝试从代码中删除它。
回答by jeni
try this
尝试这个
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('your url');
Alternatively, you can use the file_get_contents
function remotely, but many hosts don't allow this
或者,您可以file_get_contents
远程使用该功能,但许多主机不允许这样做
$userAgent = ‘Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
Some other options I use:
我使用的其他一些选项:
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
try this:
尝试这个:
$ctx = stream_context_create( array(
'socket' => array(
'bindto' => '192.168.0.107:0',
)
));
$c= file_get_contents('http://php.net', 0, $ctx);