博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php http 请求类
阅读量:6581 次
发布时间:2019-06-24

本文共 6770 字,大约阅读时间需要 22 分钟。

hot3.png

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

转载于:https://my.oschina.net/godareyou/blog/506552

你可能感兴趣的文章
我的友情链接
查看>>
38线程1-Thread-local-Timer
查看>>
为Exchange server 2013 申请多域名证书
查看>>
处理svn的 File '/aa' is out of date
查看>>
解决 Ubuntu 16.04 LTSSublime text3中文问题
查看>>
mysql主从复制实现数据库同步
查看>>
面试-1
查看>>
CentOS自动登录Gnome
查看>>
第一章,重点总结
查看>>
LeetCode - 49. Group Anagrams
查看>>
移动前端不得不了解的html5 head 头标签
查看>>
Tomcat 服务器性能优化
查看>>
【框架学习】ibatis DAO框架分析
查看>>
ZOJ 3640 Help Me Escape
查看>>
putty与emacs
查看>>
C#下实现的半角转与全角的互转
查看>>
PreparedStatement vs Statement
查看>>
使用texturePaker批量转化pvr为pn
查看>>
自我介绍
查看>>
截取指定网站Html编码
查看>>