PHP 如果获取接口的值和response headers内容
2017-08-15 22:58
453 查看
通过
2.通过Curl方式
如果需要看其他 cURL 传输选项:PHP: curl_setopt - Manual
file_get_contents获取接口信息
/** * 获取接口内容及相应headers详情 * @param string $url 需要请求的url * @return array */ public function getApiContents($url) { $content = file_get_contents($url); // 如果想设置头信息或非GET请求等 $opts = array( 'http' => array( 'method' => 'POST', // 请求方式 'header' => "Content-Type: text/xml\r\n"."Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n", // 请求头内容 'content' => $body, // 传递的内容 'timeout' => 60 ) ); // 创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值。 $context = stream_context_create($opts); $content = file_get_contents($url, false, $context, -1, 40000); // 后俩参数是设定截取字符串部分 $responseInfo = $http_response_header; // $responseInfo 输出结果 //Array //( // [0] => HTTP/1.1 200 OK // [1] => Server: ASP.NET Development Server/11.0.0.0 // [2] => Date: Fri, 11 Aug 2017 09:07:16 GMT // [3] => X-AspNet-Version: 4.0.30319 // [4] => token: WaseLE/13V+xGjiHLorGQqGy3KfOJ // [5] => Cache-Control: no-cache // [6] => Pragma: no-cache // [7] => Expires: -1 // [8] => Content-Length: 0 // [9] => Connection: Close //) // 转换成键值对格式,方便使用 $responseArray = []; foreach ($responseInfo as $response) { $list = explode(":", $response); $responseArray[$list[0]] = $list[1]; } $result = array($content, $responseArray); return $result; }
2.通过Curl方式
/** * 获取接口内容及相应headers详情 * @param string $url 请求的API地址 * @param array $post POST所需提交的数据 * @param string $token 验证的TOKEN,放header里 * @param bool $returnHeader 是否需要查看response header内容 * @return array */ public function getApiContents($url, $post = array(), $token = '', $returnHeader = false) { // 初始化 cURL 会话 $curl = curl_init(); // curl_setopt — 设置 cURL 传输选项 curl_setopt($curl, CURLOPT_URL, $url); // 需要获取的 URL 地址,也可以在curl_init() 初始化会话的时候。 //curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');// 在HTTP请求中包含一个"User-Agent: "头的字符串。我觉得没啥用就注释了。付个链接供大家了解下User-Agent [用户代理-百度百科](https://baike.baidu.com/item/%E7%94%A8%E6%88%B7%E4%BB%A3%E7%90%86/1471005?fr=aladdin&fromid=10574244&fromtitle=user-agent) // CURLOPT_FOLLOWLOCATION TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。(注意:这是递归的,"Location: " 发送几次就重定向几次,除非设置了 CURLOPT_MAXREDIRS,限制最大重定向次数。)。 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // TRUE 时将根据 Location: 重定向时,自动设置 header 中的Referer:信息。 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //在HTTP请求头中"Referer: "的内容。 //curl_setopt($curl, CURLOPT_REFERER, "http://XXX"); // 如果有数据需要存 改 一般使用POST,所以通过这种方式判断是否是POST传输 if(count($post) > 0) { // CURLOPT_POST TRUE 时会发送 POST 请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种。 curl_setopt($curl, CURLOPT_POST, 1); // 如果value是一个数组,Content-Type头将会被设置成multipart/form-data。由于这个影响了接收,我把数组转了下,http_build_query:生成 URL-encode 之后的请求字符串 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post)); } // 我的API请求规则是,已登录会在response header里返回个token,在其他请求的时候需要带上这个token if(!empty($token)) { $header = ['token: '.$token]; //设置一个你的浏览器的header curl_setopt($curl, CURLOPT_HTTPHEADER, $header); } /* 如果不设置这个则无法获取response header内容 */ curl_setopt($curl, CURLOPT_HEADER, $returnHeader); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 执行 cURL 会话 $data = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if (curl_errno($curl< b05e /span>)) { return curl_error($curl); } // 关闭 cURL 会话 curl_close($curl); $info['code'] = $httpCode; if($returnHeader){ list($header, $body) = explode("\r\n\r\n", $data, 2); /* 本来只打算要取个token的,后来觉得还是把所有的都接收下好一点,也许后来会有新的需要呢 */ //preg_match("/token\:(.*?)\n/", $header, $matches); //$info['token'] = trim($matches[1]); $headers = explode("\r\n", $header); $headList = array(); foreach ($headers as $head) { $value = explode(':', $head); $headList[$value[0]] = $value[1]; } $info['header'] = $headList; $info['content'] = $body; }else{ $info['content'] = $data; } return $info; }
如果需要看其他 cURL 传输选项:PHP: curl_setopt - Manual
相关文章推荐
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容
- PHP 如果获取接口的值和response headers内容