@xxliixin1993
2015-12-09T15:08:47.000000Z
字数 4128
阅读 1951
php
socket流程图
简单例子(模拟GET请求):
<?php
$url = "http://jk.campus.fang.com/GetPositionList_V1.api?returntype=2";
print_r(parse_url($url));
//把url拆分成数组
$info = parse_url($url);
//获取socket句柄
$fp = fsockopen($info['host'], 80, $errno, $errstr, 10);
//超时时间设置
stream_set_timeout($fp, 10);
$head = "GET ". $info['path'] .'?'. $info['query'] ." HTTP/1.0 \r\n";
$head .= "Host: ".$info['host']."\r\n";
$head .= "Content-Type: application/x-www-form-urlencoded\r\n";
$head .= "\r\n";
//向socket中写http请求头
$write = fwrite($fp, $head);
while (!feof($fp))
{
//读出http响应
$content = fread($fp,409600);
echo $content;
}
封装套接字类:
class HttpSocket {
public $host;//主机名
public $path;//请求文件
public $query;//请求参数和值
public $timeout; //超时时间 单位s
public $port; //端口
public $response;//响应内容
/**
* @param string $url 类似http://jk.campus.fang.com/GetPositionList_V1.api?returntype=2 必须要有http://
* @param int $timeout 超时时间 单位s
*/
public function __construct($url, $timeout, $port=80){
//拆分url
$info = parse_url($url);
$this->host = $info['host'];
$this->path = $info['path'];
$this->query = $info['query'];
$this->timeout = $timeout;
$this->port = $port;
}
/**
* 模拟GET请求
* @param string $request http请求头
* @return void
*/
public function getMethod($request=''){
$fp = fsockopen($this->host, $this->port, $errno, $errstr, 10);
//超时时间设置
stream_set_timeout($fp, $this->timeout);
if (!empty($this->path) && !empty($this->query)) {
$head = "GET ". $this->path .'?'. $this->query ." HTTP/1.0 \r\n";
} else if (!empty($this->path) && empty($this->query)) {
$head = "GET ". $this->path ." HTTP/1.0 \r\n";
}
$head .= "Host: ".$this->host."\r\n";
$head .= "Content-Type: application/x-www-form-urlencoded\r\n";
//$head .= "Accept-Encoding: compress\r\n";
$head .= "Connection: Close\r\n";
if (!empty($request)) {
$head .= $request;
}
$head .= "\r\n";
//向socket中写http请求头
fwrite($fp, $head);
while (!feof($fp)){
$this->response .= fread($fp,409600);
//从封装协议文件指针中取得报头
$info = stream_get_meta_data($fp);//print_r($info);exit;
if ($info['timed_out']) {
echo "time out";
@fclose($this->fp);
return false;
}
}
fclose($fp);
print_r($this->response);exit;
}
/**
* 处理得到响应body
*/
public function getResponseBody($response = ''){
if (!empty($response)) {
$this->response = $response;
}
//因为在http响应头最后是\r\n\r\n,而且只有一个
$tmp = split("\r\n\r\n" , $this->response); //print_r($this->response);exit;
if (is_array($tmp) && count($tmp) > 1) {
//当有多个\r\n时
for ($i=1; $i<count($tmp); $i++) {
$responseBody .= $tmp[$i] . "\r\n";
}
} else {
$responseBody = $tmp[0];
}
//echo $responseBody;exit;
//TODO根据响应头,检查处理Content-Type,Content-Encoding等
return $responseBody;
}
/**
* 获取响应头信息
*/
public function getResponseHeader($response = ''){
if (!empty($response)) {
$this->response = $response;
}
//因为在http响应头最后是\r\n\r\n,而且只有一个
$content = strstr($this->response, "\r\n\r\n");
//取出响应头部信息
$header = substr($this->response, 0, strlen($this->response)-strlen($content));
//把响应头转换成数组
$header = explode("\r\n", $header);
return $header;
}
}
//测试使用
$url = "http://jk.campus.fang.com/GetPositionList_V1.api?returntype=2";//返回xml
$url = 'http://jk.campus.fang.com/GetPositionList_V1.api';//返回json
//$url ='http://jk.ask.fang.com/Interface/GetHotTagsForHome.aspx';
//实例化HttpSocket类 并传入url和设置超时时常
$socket = new HttpSocket($url, 10);
//调用getMethod去模拟GET请求
$socket->getMethod();
//获取响应body
$body = $socket->getResponseBody();
print_r($body);exit;
输出信息
{"ServerTime":"2015-11-18 09:26:00","Message":"","Content":{"Item":[{"ID":"3q8ldj32jg0g0000f8a7aff7c4834307","Name":"电商特种兵:电商营销\t\/营销支持","CreateTime":"2015-09-29 17:51:08","Classify":"1"},{"ID":"3q8ldklkqov00000e56ff68d63b74d90","Name":"产品技术类(北京):产品\/工程师\/设计师","CreateTime":"2015-09-29 17:51:26","Classify":"1"},{"ID":"3q8ldlokbqk00000ea5e817e206343c4","Name":"运营支持类:分析师\/编辑\/策划","CreateTime":"2015-09-29 17:51:38","Classify":"1"},{"ID":"3q8ldmsm42u000007720fc2f875e44c3","Name":"职能管理类:财务\/人事\/行政","CreateTime":"2015-09-29 17:51:51","Classify":"1"},{"ID":"c78hc08du6c0000fc2582111ef3457da","Name":"电商特种兵:电商营销\t\/营销支持","CreateTime":"2015-09-29 17:52:04","Classify":"2"},{"ID":"c78hc58k0b40000d5deac4ed6de469db","Name":"产品技术类(北京):产品\/工程师\/设计师","CreateTime":"2015-09-29 17:52:22","Classify":"2"},{"ID":"c78hc80n2js00004e6a3e7e64774196a","Name":"运营支持类:分析师\/编辑\/策划","CreateTime":"2015-09-29 17:52:32","Classify":"2"},{"ID":"c78hcb7v6880000aab38688a977428e9","Name":"职能管理类:财务\/人事\/行政","CreateTime":"2015-09-29 17:52:43","Classify":"2"}]}}