function httpPost($url, $data,$cookieStr='') { $url_array = parse_url($url); $host = $url_array['host']; $port = isset($url_array['port'])?($url_array['port']):80; if(!($conn = fsockopen($host,$port,$errno, $errstr, 30))) { return false; } $header = "POST ".$url." HTTP/1.1\r\n"; $header.= "Host : {$host}\r\n"; $header.= "Content-type: application/x-www-form-urlencoded\r\n"; $header.= "Content-Length:".strlen($data)."\r\n"; $header.= "Connection: close\r\n"; //這裏是用來寫cookie的 if (!empty($cookieStr)) { $header.="Cookie: ".$cookieStr."\r\n"; } //注意下面開頭還加了個換行,結尾是兩個換行 $header.= "\r\n{$data}\r\n\r\n"; //寫數據 fwrite($conn,$header); //這裏讀cookie $cookieStr=array(); //下面的判斷,讀到空行時,說明頭已經結束了,接下來是內容。 while( ($line=trim(fgets($conn))) != "" ) { $header.=$line; /* */ if(strstr($line,"Set-Cookie:")) { list($coo,$cookieLine)=explode(" ",$line); $cookieStr[] = $cookieLine; } } //if($len <= 0) //{ // return false; // } //讀數據 //$body=fread($conn,$len); while (!feof($conn)) { $body .= fread($conn, 8192); } fclose($conn); $result['body'] = $body; $result['cookieArr'] = $cookieStr; return $result; }
轉自:http://baiyuxiong.iteye.com/blog/786214php