PHP httpRequest实现http https get post postjson postxml 获取响应头 获取响应cookie(待优化)

话不多说直接上代码,可能不是很全面,有可以完善的点可以评论一下呀

    /** 发送httpRequest请求
     * @param string $url 请求地址
     * @param bool|null $isSSL 是否ssl协议
     * @param array|null $getParam get请求参数
     * @param array|null $postParam post请求参数(json或者xml数组 会自动转换)
     * @param array|null $headers 请求头
     * @param array|null $cookies 请求cookie
     * @param int|null $requestType 请求类型 1get 2post 3postjson 4postxml
     * @return array|null code http状态码,msg错误信息,body响应正文,header响应头,cookie响应cookie
     */
function httpRequest(string $url,
                             ?bool $isSSL = true,
                             ?array $getParam = [],
                             ?array $postParam = [],
                             ?array $headers = [],
                             ?array $cookies = [],
                             ?int $requestType = 1): ?array
 {
     if (!function_exists('xml_encode')) {
         /** XML编码
          * @param array $data xml数据数组
          * @return string
          */
         function xml_encode(array $data)
         {
             $str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
             $str .= build_xml_str($data);
             return $str;
         }
     }
     if (!function_exists('build_xml_str')) {
         /** 构建xml node
          * @param array $data xml node数据
          * @return string
          */
         function build_xml_str(array $data)
         {
             $str = '';
             foreach ($data as $k => $v) {
                 if (!is_array($v)) {
                     $str .= "<{$k}>{$v}</{$k}>\n";
                 } else {
                     $str .= "<{$k}>\n" . build_xml_str($v) . "</{$k}>\n";
                 }
             }
             return $str;
         }
     }


     $resultArr = ['code' => 0, 'msg' => 'url错误', 'body' => [], 'header' => [], 'cookie' => []];
     if (empty($url)) {
         return $resultArr;
     }
     $curl = curl_init();

     //组合get请求地址
     if (!empty($getParam)) {
         $url .= '?' . http_build_query($getParam);
     }

     //设置请求地址
     curl_setopt($curl, CURLOPT_URL, $url);
     //设置不输出
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     //设置cookies
     if (!empty($cookies)) {
         curl_setopt($curl, CURLOPT_COOKIE, $cookies);
     }
     //设置ssl请求方式
     if ($isSSL) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
     }

     //设置请求数据
     if ($requestType > 1) {
         curl_setopt($curl, CURLOPT_POST, true);//post提交方式
         if (!empty($postParam)) {
             curl_setopt($curl, CURLOPT_POSTFIELDS, $postParam);
         }
     }
     switch ($requestType) {
         case 3:
             //httpPost JSON格式请求
             $headers['Content-Type'] = 'application/json; charset=utf-8';
             $json = json_encode($postParam);
             $headers['Content-Length'] = strlen($json);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
             break;
         case 4:
             //httpPost XML格式请求
             $headers['Content-Type'] = 'text/xml; charset=utf-8';
             $xml = xml_encode($postParam);
             $headers['Content-Length'] = strlen($xml);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
             break;
     }

     curl_setopt($curl, CURLOPT_HEADER, 1);
     //设置请求头
     if (!empty($headers)) {
         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     }
     $contents = curl_exec($curl);
     // 获得响应结果里的:头大小
     $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     // 根据头大小去获取头信息内容
     $header = substr($contents, 0, $headerSize);
     preg_match_all("/(?<=set\-cookie:)[^\r\n]+/i", $header, $matches);
     $resultArr = ['code' => $code, 'msg' => '', 'body' => substr($contents, $headerSize, strlen($contents)), 'header' => $header, 'cookie' => $matches];
     return $resultArr;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值