/** * post提交数据 * @param string $url 请求Url * @param array $datas 提交的数据 * @return url响应返回的html */ function sendPost($url, $datas) { $temps = array(); foreach ($datas as $key => $value) { $temps[] = sprintf('%s=%s', $key, $value); } $post_data = implode('&', $temps); $url_info = parse_url($url); if(empty($url_info['port'])) { $url_info['port']=80; } $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n"; //get请求,这里就改为get $httpheader.= "Host:" . $url_info['host'] . "\r\n"; $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n"; $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n"; //描述HTTP消息实体的传输长度 $httpheader.= "Connection:close\r\n\r\n"; $httpheader.= $post_data; $fd = fsockopen($url_info['host'], $url_info['port']); //post请求 fwrite($fd, $httpheader); $gets = ""; $headerFlag = true; while (!feof($fd)) { //检测是否到达了文件末尾,如果服务器没有关闭fsockopen,feof会等到超时 默认的超时限制是 60 秒,可以使用 stream_set_timeout() 来改变这个值。 if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) { break; } } while (!feof($fd)) { $gets.= fread($fd, 128); //如果只是想将一个文件的内容读入到一个字符串中,请使用 ,它的性能比 fread() 好得多 } fclose($fd); return $gets; }
亲测好用