php模拟http报文,调用webservice
使用php模拟http报文,作post请求:
当然在这里主要用于,调用各种webservice接口,可以写入token,等信息,模拟保温函数,和调用方法如下:
// 调用请求函数
public function test(){
$chost = 'test';
$cpath = 'http://www.cleey.com';
$crequest = '';
$rel = $this->http_post($crequest, $chost, $cpath,80);
var_dump($rel);
}
// 模拟http post请求
function http_post($request, $host, $path, $port) {
$strlen = strlen($request);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Accept-Encoding: gzip,deflate\r\n";
$http_request .= "Content-Type: application/json;charset=UTF-8\r\n";
$http_request .= "User-Agent: Jakarta Commons-HttpClient/3.1\r\n";
$http_request .= "Host: www.cleey.com\r\n";
$http_request .= "Content-Length: $strlen\r\n";
// $http_request .= "Token: wwwww\r\n";
$http_request .= "\r\n";
$http_request .= $request;
$response = '';
if( false != ( $fs = @fsockopen( $host, $port, $errno, $errstr, 10 ) ) ) {
fwrite( $fs, $http_request );
while ( !feof( $fs ) )
$response .= fgets( $fs, 1160 ); // One TCP-IP packet
fclose( $fs );
$response = explode( "\r\n\r\n", $response, 2 );
}
return $response;
}