分享
 
 
 

我的HTTP Client 模拟PHP Class

王朝学院·作者佚名  2009-07-02
窄屏简体版  字體: |||超大  

最近因为要通过PHP调用REST接口,简单的写了一个模拟HTTPClient的PHP Class.相当于模拟浏览器操作.这是一个初始版本.等以后有时间了在完善下.如果有什么意见或者好的想法,欢迎指教.

<?php

/*

===========================================================

HTTP.php Zhu Weiwei

Copyright © 2006-2008

http://www.cncmm.com China. All Rights Reserved.

===========================================================

*/

class HTTP{

private $m_httpVersion = "HTTP/1.0";

private $m_method = "GET";

private $m_enctype = "application/x-www-form-urlencoded";

private $m_scheme = "http";

private $m_host = "127.0.0.1";

private $m_port = 80;

private $m_path = "/";

private $m_query = "";

private $m_fragment = "";

private $m_user = "";

private $m_pass = "";

private $m_uri = "/";

private $m_referer = "";

private $m_mimeBoundary = "";

private $m_sockTimeout = 10;

private $m_readTimeout = 10;

private $m_timeout = false;

private $m_sock = "";

private $m_error = "";

private $CRLF = "\r\n";

private $m_arrParam = Array();

private $m_arrFile = Array();

private $m_arrCookie = Array();

private $m_request = "";

private $m_requestHead = "";

private $m_requestBody = "";

private $m_response = "";

private $m_responseHead = Array();

private $m_responseBody = "";

private $m_maxLineLength= 1024;

function __construct() {

}

function getResponseHead() {

return $this->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 = '<'.base64_encode(uniqid(microtime(),true)).'=>';

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

<?php

require_once("http.php");

$oHTTP = new HTTP();

$oHTTP->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);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有