php中 http 类,php http 请求类

//调用方法

require 'cls_php.php';

$httpRequest = new cls_http();

$url="http://localhost/post.php";

$parameters = array(

username =>'user1',

password => 'passwd1'

);

$returnContent=$httpRequest->Http($url,'POST',$parameters);

print($returnContent);

class cls_http

{

public $errorMsg = array();

public $httpStateCode;

public $url;

public $timeOut = 30;

public $connectTimeOut = 30;

public $sslVerifyPeer = false;

public $httpInfo;

public static $boundArray = '';

function Http($url,$method = 'GET',$parameters = array(),$multi = false)

{

switch ($method)

{

case 'GET':

if (!empty($parameters))

{

$url = $url .'?'.http_build_query($parameters);

}

return $this->httpRequest($url, $method);

break;

case 'POST':

if(is_array($parameters))

{

$body = $this->GetParams($parameters);

}

else

{

$body=$parameters;

}

return $this->HttpRequest($url, $method,$body);

break;

default:

$headerArray = array();

if(   !$multi && ( is_array($parameters) || is_object($parameters) )   )

{

$body = http_build_query($parameters);

}

else

{

$body = self::Build_http_query_multi($parameters);

$headerArray[] = "Content-Type: mutipart/form-data;boundArray=".self::$boundArray;

}

return $this->HttpRequest($url, $method,$body,$headerArray);

break;

}

}

function HttpRequest ($url,$method,$postFields = NULL,$headerArray = array())

{

if(!function_exists('curl_init'))

{

if(function_exists('fsockopen'))

{

$responseContent = $this->FsockRequest($url, $method,$postFields,$headerArray);

return $responseContent;

}

return false;

}

$this->httpInfo=array();

$ch = curl_init();

$options = array(

CURLOPT_CONNECTTIMEOUT => $this->connectTimeOut ,

CURLOPT_TIMEOUT  => $this->timeOut,

CURLOPT_RETURNTRANSFER => TRUE,

CURLOPT_ENCODING => '',

CURLOPT_SSL_VERIFYPEER => $this->sslVerifyPeer,

CURLOPT_HEADERFUNCTION => array($this,'getHeader'),

CURLOPT_HEADER,FALSE

);

curl_setopt_array($ch, $options);

if ($method == 'POST')

{

if(!empty($postFields))

{

curl_setopt($ch, CURLOPT_POST, TRUE );

curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

$this->postData = $postFields;

}

}

curl_setopt($ch, CURLOPT_URL, $url );

curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray );

curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE );

$responseContent = curl_exec($ch);

$this->httpStateCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);

$this->httpInfo = array_merge($this->httpInfo,curl_getinfo($ch));

$this->url=$url;

curl_close($ch);

return $responseContent;

}

/**

* Get the header info to store.

*

* @return int

* @ignore

*/

function getHeader($ch, $header) {

$i = strpos($header, ':');

if (!empty($i)) {

$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));

$value = trim(substr($header, $i + 2));

$this->http_header[$key] = $value;

}

return strlen($header);

}

public static function Build_http_query_multi($params)

{

if(!$params) return '';

uksort($params,'strcmp');

$pairs = array();

self::$boundArray = $boundArray = uniqid('-----------------');

$MPboundArray = '--'.$boundArray;

$endMPboundArray = $MPboundArray . '--';

$multipartbody = '';

foreach ($params as $parameter => $value)

{

if (in_array($parameter,array('pic','image')) && $value{0} == '@')

{

$url = ltrim( $value, '@' );

$content = file_get_contents( $url );

$array = explode( '?', basename( $url ) );

$filename = $array[0];

$multipartbody .= $MPboundArray . "\r\n";

$multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "\r\n";

$multipartbody .= "Content-Type: image/unknown\r\n\r\n";

$multipartbody .= $content. "\r\n";

}

else

{

$multipartbody .= $MPboundArray . "\r\n";

$multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";

$multipartbody .= $value."\r\n";

}

}

$multipartbody .= $endMPboundArray;

return $multipartbody;

}

function GetParams($p)

{

$str = '';

foreach ($p as $key => $value)

{

if(isset($str{1}))

{

$str .= '&';

}

$str .= $key .'='.$value;

}

return $str;

}

function FsockRequest($url,$method,$postFields = NULL,$headerArray = array())

{

$urlArray = parse_url($url); //解析 URL,返回其组成部分

$errNo    = '';

$errStr = '';

$transPorts = '';

$responseContent = '';

if ($urlArray['schme'] === 'https')

{

$transPorts = 'ssl://';

$urlArray['port'] = '443';

}

else

{

$transPorts = 'tcp://';

$urlArray['port'] = '80';

}

$fp = fsockopen($transPorts.$urlArray['host'],$urlArray['port'],$errNo,$errStr,$this->timeOut);

if(!$fp){

die("ERROR:$errNo -". $errStr);

return false;

}

else {

if(!empty($urlArray['query']))

{

$urlArray['path'] .= '?'.$urlArray['query'];

}

$urlArray['method'] = $method;

$header = $method.' '.$urlArray['path']." HTTP/1.1\r\n";

if ($method == 'POST')

{

$header .= "Content-type: application/x-www-form-urlencoded\r\n";

$header .= "Content-length: ".strlen($postFields)."\r\n";

}

$header .= 'Host: '.$urlArray['host']."\r\n";

$header .=  "Connection: close\r\n\r\n";

fputs($fp,$header);

if($method == 'POST')

{

fputs($fp,$postFields."\r\n\r\n");

}

while(!feof($fp)){

$responseContent .= @fgets($fp,4096);

}

fclose($fp);

$responseContent =substr( stristr($responseContent,"\r\n\r\n"),strlen("\r\n\r\n"));

return $responseContent;

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值