Maison > Article > développement back-end > 求解惑 , php向其它服务器请求时经常响应失败.该怎么解决
求解惑 , php向其它服务器请求时经常响应失败.
1 直接用file_get_contents 来实现
file_get_contents( $url );
2 使用curl 扩展.
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
curl_close($ch);
3 socket 套接.
$address = ' http://1.2.3.4:9001/';
$path = '/aaa/bbb';
$sock = fsockopen($address, 80, $errno, $errstr, 30);
if (!$sock) die("$errstr ($errno)\n");
$psw = md5('123.456');
$data = "xxx=xxx&yyy=yyy";
fwrite($sock, "POST $path HTTP/1.0\r\n");
fwrite($sock, "Host: localhost\r\n");
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, "$data\r\n");
fwrite($sock, "\r\n");
$headers = "";
while ($str = trim(fgets($sock, 4096)))
$headers .= "$str\n";
$body = "";
while (!feof($sock))
$body .= fgets($sock, 4096);
fclose($sock);
$body1 = explode("#",$body);
return $body1;
三种方式均效果缓慢。
直接输出 echo ""; 时正常.
上述代码均在for 循环中实际运行。下面的抓包的效果
之前访问都挺好的,最近,使用时,经常卡住。会报错
求各位朋友帮忙!
------解决方案--------------------
1、考虑是否 cookie 的影响
2、应该设置来路
都是内网地址,把图涂得那么乱干什么
------解决方案--------------------
源地址的网络不好?
------解决方案--------------------