分享一段CurvePHP中的socket实现http请求类
<?php
/**
* OpenSource code of CurvePHP framework
* Create by Curve WorkRoom Group at 2010
* http://curvephp.xiao688.com/
* 处理http协议交互内容
*/
class socket_http{
public $timeout=30;
protected $url;
protected $url_info=array();
protected $http_header=array();
protected $response_status=array();
protected $response_header=array();
protected $response_data;
protected $cookie_file='socket_http_cookie.txt';
public $save_cookie=true;
public $http_method='GET';
public $http_protocol='HTTP/1.1';
function __construct($userAgent='firefox'){
//这些初始化一些默认user-Agent设置
$defaultAgent=array(
'chrome'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7',
'firefox'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12',
'IE'=>'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)'
);
if(array_key_exists($userAgent,$defaultAgent)){
$this->http_header['User-Agent']=$defaultAgent[$userAgent];
}
$this->http_header['Accept']='*/*';
$this->http_header['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$this->http_header['Accept-Language']='zh-cn,zh;q=0.5';
$this->http_header['Accept-Encoding']='deflate';
$this->http_header['Accept-Charset']='GB2312,utf-8;q=0.7,*;q=0.7';
$this->http_header['Keep-Alive']='115';
$this->http_header['Connection']='close';
}
function __get($variable){
if($variable=='data'){
return $this->response_data;
}
}
/**
* 通过socket 进行一次url请求
*/
function fetch($url){
return $this->request($url);
}