我的网址“www.example.com”正在浏览器中工作,但当我通过网址“www.example.com”收到回复时,我收到503服务无法回复。
我使用了以下代码:
$url = 'http://www.example.com';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$JsonResponse = curl_exec($curl_handle);
$http_code = curl_getinfo($curl_handle);
print_r($http_code);die;
答案 0 :(得分:0)
我很确定远程服务器需要特定的HTTP标头(例如cookie),例如会话令牌或语言首选项。
您必须分析从浏览器发送到远程服务器的HTTP流量,并自行查找所需的HTTP标头。我推荐像Fiddler这样的工具。
一个例子:
GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: foo=bar
Connection: keep-alive
假设远程服务器要求客户端发送名为foo
的cookie,如果省略它,他可能会向您发送503或400错误消息。您还必须从cURL发送cookie以获得成功的响应,就像常规客户一样。