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/786214