时间:2021-07-01 10:21:17 帮助过:26人阅读
$matches = parse_url($url); !isset($matches['host']) && $matches['host'] = ''; !isset($matches['path']) && $matches['path'] = ''; !isset($matches['query']) && $matches['query'] = ''; !isset($matches['port']) && $matches['port'] = ''; $host = $matches['host']; $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'; $port = !empty($matches['port']) ? $matches['port'] : 80; if($post) { $out = "POST $path HTTP/1.0\r\n"; $out .= "Accept: */*\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "Content-Type: application/x-www-form-urlencoded\r\n"; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; $out .= "Host: $host\r\n"; $out .= 'Content-Length: '.strlen($post)."\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cache-Control: no-cache\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; $out .= $post; } else { $out = "GET $path HTTP/1.0\r\n"; $out .= "Accept: */*\r\n"; $out .= "Accept-Language: zh-cn\r\n"; $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n"; $out .= "Host: $host\r\n"; $out .= "Connection: Close\r\n"; $out .= "Cookie: $cookie\r\n\r\n"; } $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); if(!$fp) { return '';//note $errstr : $errno \r\n } else { stream_set_blocking($fp, $block); stream_set_timeout($fp, $timeout);var_dump($fp); @fwrite($fp, $out); //注释标记 /*$status = stream_get_meta_data($fp); if(!$status['timed_out']) { while (!feof($fp)) { if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { break; } } $stop = false; while(!feof($fp) && !$stop) { $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); $return .= $data; if($limit) { $limit -= strlen($data); $stop = $limit <= 0; } } }*/ //注释标记 @fclose($fp); return true; }
/* 对于楼主的应用, 这里得到的是TCP套接字的一些信息 */ $status = stream_get_meta_data($fp); /* 这里是检查TCP连接是否超时 */ if(!$status['timed_out']) { /* 如果没有超时, 那么就读取数据, 直到碰到文件结束符 */ while (!feof($fp)) { /* 由于这里是读取HTTP头信息, 空行标识头信息结束, 因此需要break */ if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) { break; } } /* 这里是读取HTTP的body, 个人认为这里的处理有一些粗糙, 至少先看看HTTP响应头中有没有Content-Length根据它进行处理, 如果没有再如下读取 */ $stop = false; while(!feof($fp) && !$stop) { /* 读取最多8192字节($limit递减) */ $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); /* 将读取到的内容连接到返回字符串中 */ $return .= $data; if($limit) { $limit -= strlen($data); $stop = $limit <= 0; } } }
------解决方案--------------------