function http($host, $path, $post, $timeOut = 60) {
if (function_exists ( 'curl_init' ) && function_exists ( 'curl_exec' )) {
$ch = curl_init ( 'http://' . $host . $path );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, $timeOut );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post );
$body = curl_exec ( $ch );
curl_close ( $ch );
return $body;
} else {
$values = array ();
foreach ( $post as $key => $value ) {
$values [] = "$key=" . urlencode ( $value );
}
$data = implode ( '&', $values );
$header = "POST " . $path . " HTTP/1.0\r\n"; // 使用HTTP/1.1协议内容会有异常
$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";
$header .= "\r\n";
$header .= $data . "\r\n";
$body = '';
$fp = fsockopen ( $host, 80, $errno, $errstr, $timeOut );
fputs ( $fp, $header );
while ( ! feof ( $fp ) ) {
$body .= fgets ( $fp, 128 );
}
fclose ( $fp );
return substr ( $body, strpos ( $body, "\r\n\r\n" ) + 4 );//fsockopen去除头部header关键步骤
}
}
//http://my.oschina.net/cart/
$content = http ( 'www.oschina.net', '/', array ('k1' => 111, 'k2' => 2222) );
var_dump($content);
exit ();