//调用方法
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;
}
}
}
?>