PHP

Http请求类

字号+ 编辑: 国内TP粉 修订: 种花家 来源: ThinkPHP 2023-09-11 我要说两句(0)

网络转载。

自己常用的一个http请求类,和常用的区别是假如对方返回数据有utf8的bom头会自动去掉,防止解析数据出错

经常对接接口的同学可能碰到过一个问题:就是明明看着对方返回的数据是对的,可json_decode解码总是出错。那其中一个原因可能就是:对方返回的数据是utf8的并且带着BOM头

此代码特别的地方就是,假如有bom头会自动去掉。

以下是

源代码

class HttpSvc
{
    const TIME_OUT = 3;
    static  $ALL_CURL = array('post','get');
    public static function httpReqUrl($url,$method='get',$timeout=1)
    {
        if(!in_array(strtolower($method),self::$ALL_CURL))
            return $url;
        if(!$url)
            return ;
        $param = array();
        $urlarr = explode("?",$url,2);
        $url = $urlarr[0];
        if(isset($urlarr[1]) && !empty($urlarr[1])) {
            parse_str($urlarr[1],$param);
        }
        $res=self::HttpReqArr($url,$param,$timeout,strtolower($method));
        return $res['data'];
    }
    public static function HttpReqArr($url,$opts=array(),$timeout=3,$mothod='post',$proxy=null)
    {
        $data=array();
        //  $logger = Logger::ins();
        try{
            $stime=microtime(true);
            $url = trim($url);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            if($proxy)
                curl_setopt($ch,CURLOPT_PROXY,$proxy);
            if(isset($opts) && count($opts)>0)
            {
                $pstring='';
                foreach($opts as $key => $val)
                    $pstring .= trim($key) . '=' . urlencode(trim($val)) . "&";
                $pstring = substr($pstring,0,-1);
                if(strtolower($mothod)=='post') {
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $pstring);
                    curl_setopt($ch, CURLOPT_URL, $url);
                }
                else
                {
                    curl_setopt($ch, CURLOPT_HTTPGET, true);
                    curl_setopt($ch, CURLOPT_URL, $url."?".$pstring);
                }
            }
            else
            {
                if(strtolower($mothod)=='post')
                    curl_setopt($ch, CURLOPT_POST, true);
                else
                    curl_setopt($ch, CURLOPT_HTTPGET, true);
                curl_setopt($ch, CURLOPT_URL, $url);
            }
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            $r = curl_exec($ch);
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
            $etime=microtime(true);
            $usetime=$etime-$stime;
            // $logger->debug("[response] code: $http_code, usetime: $usetime, url:$url");
            $error = curl_error($ch);
            if($error){
                //  $logger->error("code: $http_code, usetime: $usetime, callbackUrl:$url?$pstring method:$mothod  opt=".var_export($opts,true).'&proxy='.$proxy);
            }
        }catch(Exception $e) {
            //  $logger->error("code: $http_code, usetime: $usetime, callbackUrl:$url?$pstring method:$mothod  opt=".var_export($opts,true).' exception '.$e->getMessage()."&proxy=$proxy");
        }
        curl_close($ch);
        $r = self::RemoveBom($r);
        $data=array('code'=>$http_code,'data'=>$r);
        //  $logger->info("code: $http_code, usetime: $usetime, method:$mothod,callBackUrl:$url?$pstring ,putData:".$r.",proxy=$proxy");
        return $data;
    }
    public static function RemoveBom($content)
    {
        if(substr($content, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
            $content=substr($content, 3);
        }
        return $content;
    }
}
阅完此文,您的感想如何?
  • 有用

    1

  • 没用

    0

  • 开心

    1

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • mac开发接入微信公众号接口返回报错 cURL error 56: SSLRead() return error -9806

  • pecl安装程序时报错Array and string offset access syntax with curly braces is no longer supported

  • PHP的换行符是什么

  • 由于商家传入的H5交易参数有误,该笔交易暂时无法完成,请联系商家解决

我要说说
网上宾友点评