cleey
望着那一丝海线,若隐若现。落日下的海霞,数不尽的美,看不完的醉
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;
}
<< 上一篇 Linux 搭建邮件服务器 之 Postfix 安装 js获取浏览器类型 下一篇 >>
文章标签
随意 | Created At 2014 By William Clinton | Contact Me: 1619488373@qq.com | 蜀ICP备14002619号 |