最近因为要通过php调用rest接口,简单的写了一个模拟httpclient的php class.相当于模拟浏览器操作.这是一个初始版本.等以后有时间了在完善下.如果有什么意见或者好的想法,欢迎指教.
m_responsehead;
}
function getresponsebody() {
return $this->m_responsebody;
}
function getresponse() {
return $this->m_response;
}
function getrequesthead() {
return $this->m_requesthead;
}
function getrequestbody() {
return $this->m_requestbody;
}
function getrequest() {
return $this->m_request;
}
function geterror() {
return $this->m_error;
}
function setmethod($m) {
$this->m_method = (strtoupper($m=='post'))?('post'):('get');
}
function setenctype($e) {
$this->m_enctype = (strtolower($e)=='application/x-www-form-urlencoded')?
('application/x-www-form-urlencoded'):('multipart/form-data');
}
function seturl($url) {
$rs = parse_url($url);
$this->m_scheme = empty($rs['scheme']) ?('http') :($rs['scheme']);
$this->m_host = empty($rs['host']) ?('localhost'):($rs['host']);
$this->m_port = empty($rs['port']) ?('80') :($rs['port']);
$this->m_user = empty($rs['user']) ?('') :($rs['user']);
$this->m_pass = empty($rs['pass']) ?('') :($rs['pass']);
$this->m_path = empty($rs['path']) ?('/') :($rs['path']);
$this->m_query = empty($rs['query']) ?('') :($rs['query']);
$this->m_fragment = empty($rs['fragment'])?('') :($rs['fragment']);
$this->m_uri = $this->m_path;
if(!empty($this->m_query)){
$this->m_uri .= '?'.$this->m_query;
}
if(!empty($this->m_fragment)){
$this->m_uri .= '#'.$this->m_fragment;
}
}
function addparameter($key,$val) {
if(!empty($key)){
$this->m_arrparam[$key] = $val;
}
}
function addfile($key,$file) {
if(!empty($key) && file_exists($file)){
$this->m_arrfile[$key] = $file;
}
}
function addcookie($key,$val) {
if(!empty($key)){
$this->m_arrcookie[$key] = $val;
}
}
function connect() {
$this->m_sock = fsockopen($this->m_host,
$this->m_port,
$errno,
$errstr,
$this->m_socktimeout);
if($this->m_sock){
return true;
}else{
switch($errno){
case -3: $this->m_error=socket creation failed (-3); break;
case -4: $this->m_error=dns lookup failure (-4); break;
case -5: $this->m_error=connection refused or timed out (-5); break;
default: $this->m_error=connection failed (.$errno.); break;
}
return false;
}
}
function disconnect(){
return(fclose($this->m_sock));
}
http://www.devdao.com/
function sendrequest() {
if($this->connect()){
$this->buildrequest();
socket_set_timeout($this->m_sock, $this->m_readtimeout);
fwrite($this->m_sock, $this->m_request, strlen($this->m_request));
/*
get and parse http header
*/
while( !feof($this->m_sock) )
{
if( $this->m_readtimeout > 0 && $this->checkresponsetimeout() ){
return false;
}
$line = fgets($this->m_sock, $this->m_maxlinelength);
if($line == $this->crlf )
break;
$this->m_responsehead['content'] .= $line;
if(preg_match(|^http/[^\s]*\s(.*?)\s|,$line, $status)){
$this->m_responsehead['status'] = $status[1];
}else{
list($key,$val) = explode(': ',$line,2);
$this->m_responsehead[$key] = $val;
}
}
/*
get http body
*/
while(!feof($this->m_sock) ) {
if( $this->m_readtimeout > 0 && $this->checkresponsetimeout() ){
return false;
}
$this->m_responsebody .= fgets($this->m_sock, $this->m_maxlinelength);
}
$this->m_response = $this->m_responsehead['content'].$this->m_responsebody;
$this->disconnect();
return true;
}else{
return false;
}
}
function checkresponsetimeout()
{
if ($this->m_readtimeout > 0) {
$fp_status = socket_get_status($this->m_sock);
if ($fp_status[timed_out]) {
$this->m_error=read response timeout!;
return true;
}
}
return false;
}
function buildrequest() {
switch( $this->m_method ){
case post:
$body = $this->buildrequestbody();
$head = $this->buildrequesthead();
$this->m_request = $head.$this->crlf.$this->crlf.$body;
break;
case get:
$head = $this->buildrequesthead();
$this->m_request = $head.$this->crlf.$this->crlf;
break;
}
return $this->m_request;
}
function buildrequesthead() {
$header = array();
switch( $this->m_method ){
case post:
$header[] =post $this->m_uri $this->m_httpversion;
$header[] = content-length: .strlen($this->m_requestbody);
switch ($this->m_enctype) {
case application/x-www-form-urlencoded:
$header[] =content-type: application/x-www-form-urlencoded;
//$header[] =content-type: application/xml; charset=utf-8;
break;
case multipart/form-data:
$header[] =content-type: multipart/form-data; boundary=$this->m_mimeboundary;
break;
}
break;
case get:
$header[] =get $this->m_uri $this->m_httpversion;
break;
}
$header[] = cache-control: no-cache;
$header[] = pragma: no-cache;
$header[] = user-agent: mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.0.11) gecko/2009060215 firefox/3.0.11;
$header[] = accept: */*;
#$header[] = accept-language: zh-cn;
#$header[] = accept-encoding: gzip,deflate;
#$header[] = accept-charset: utf-8;
$header[] = connection: keep-alive;
$header[] = keep-alive: 300;
//$header[] = connection: close;
//$header[] = referer: $this->m_referer;
//$header[] = cookie: .join(';',$this->m_arrcookie);
if( intval($this->m_port) == 80 ){
$header[] = host: $this->m_host;
}else{
$header[] = host: $this->m_host:$this->m_port;
}
if(!empty($this->m_user) || !empty($this->m_pass)){
$header[] = authorization: basic .base64_encode($this->m_user.:.$this->m_pass);
}
$this->m_requesthead = join($this->crlf, $header);
return $this->m_requesthead;
}
function buildrequestbody() {
$body = array();
switch ($this->m_enctype) {
case application/x-www-form-urlencoded:
foreach( $this->m_arrparam as $key=>$val) {
$body[] = urlencode($key).=.urlencode($val);
}
$this->m_requestbody = join(&,$body);
//$this->m_requestbody = json_encode($this->m_arrparam); //for rest
break;
case multipart/form-data:
$this->m_mimeboundary = '';
foreach( $this->m_arrparam as $key=>$val) {
$body[] = --.$this->m_mimeboundary;
$body[] = content-disposition: form-data; name=\$key\.$this->crlf;
$body[] = $val;
}
foreach( $this->m_arrfile as $key=>$filename) {
$body[] = --.$this->m_mimeboundary;
$body[] = content-disposition: form-data; name=\$key\; filename=\$filename\;
$body[] = content-type: application/octet-stream;
$body[] = content-transfer-encoding: binary.$this->crlf;
$body[] = file_get_contents($filename);
}
$body[] = --.$this->m_mimeboundary.--.$this->crlf;
$this->m_requestbody = join($this->crlf,$body);
break;
}
return $this->m_requestbody;
}
};
example.php
seturl(http://www.cncmm.com/);
$ohttp->setmethod(post);
$ohttp->addparameter(param,value);
$ohttp->addfile(filedata,/tmp/tmpfile);
if( !$ohttp->sendrequest() ){
print($ohttp->geterror());
exit();
}
$request = $ohttp->getrequest();
$response = $ohttp->getresponse();
print($response);