1.先用fsockopen打开一个链接
2.用fwrite写入头部信息
3.用fgets获取数据时,返回false
问题是: 1.如果加了"Content-Length:" . strlen($data) 和 $data函数fgets就返回false,而不加这两个fgets又能返回正常的数据,什么原因呢?
2.请求的host换成环境一样的测试环境时,可以正常得到返回的数据,是本地的php或者ngnix缺少什么配置导致的吗?
$httpheader = "POST " . $url . " HTTP/1.0" . "\r\n" . "Accept:*/*" . "\r\n" . "Accept-Language:zh-cn" . "\r\n" . "Referer:" . $url . "\r\n" . "Content-Type:application/x-www-form-urlencoded" . "\r\n" . "User-Agent:Mozilla/4.0(compatible;MSIE 7.0;Windows NT 5.1)" . "\r\n" . "Host:" . $host . "\r\n" . "Content-Length:" . strlen($data) . "\r\n" . "\r\n" . $data;
$fd = fsockopen($host, $port);
if (!is_resource($fd)) {
return "fsockopen failed";
}
fwrite($fd, $httpheader);
stream_set_timeout($fd, $timeout);
$info = stream_get_meta_data($fd);
$gets = "";
while ((!feof($fd)) && (!$info['timed_out'])) {
$gets .= fgets($fd, 500);
$info = stream_get_meta_data($fd);
@ob_flush();
flush();
}