php curl的post(get)请求返回header与body解析

    xiaoxiao2022-07-02  109

    /** * 获取post请求返回的header跟body部分,主要用于有返回数据在header头部的请求 * @param $url 请求地址 * @param $data 请求参数:格式:array(“user”=>“test”) * @return bool|string */

    public static function getHttpPostData($url, $data) { $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data, CURLOPT_TIMEOUT => 20 ); $http = curl_init($url); curl_setopt_array($http, $options); $sContent = curl_exec($http); if ($errNo = curl_errno($http)) { LogUtils::info("xml->rspHead::error--->" . curl_error($http)); } // 获得响应结果里的:头大小 $headerSize = curl_getinfo($http, CURLINFO_HEADER_SIZE); $headerTotal = strlen($sContent); $bodySize = $headerTotal - $headerSize; // 根据头大小去获取头信息内容 $header = substr($sContent, 0, $headerSize); $comma_separated = explode("\r\n", $header); $arr = array(); foreach ($comma_separated as $value) { if (strpos($value, ':') !== false) { $a = explode(":", $value); $key = $a[0]; $v = $a[1]; $arr[$key] = $v; } else { array_push($arr, $value); } } $body = substr($sContent, $headerSize, $bodySize); LogUtils::info("xml->header::" . json_encode($arr)); LogUtils::info("xml->body::" . $body); curl_close($http); return $body; }
    最新回复(0)