获取cURL响应标头以及响应正文

时间:2015-10-29 00:45:03

标签: php api curl http-headers imgur

我正在使用Imgur API上传图片。他们在API docs中详细说明了每个请求(当我通过他们的API上传图片时)也有响应标题,这会告诉我该帐户剩余的信用额度。

我需要返回HTTP响应标头X-RateLimit-ClientRemaining。以下是我目前用于获取cURL正文的代码:

$filename = dirname(realpath(__FILE__))."/images/$value";
$client_id = "f*************c";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$pvars   = array('image' => base64_encode($data));
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url=$pms['data']['link'];
if($url!=""){
    // add to success
    array_push($success, $url);
}
else {
    // add to fail
    $p = $value.' failed, error: '.$pms['data']['error'];
    array_push($fail, $p);
}

$value来自我未包含的循环)

1 个答案:

答案 0 :(得分:0)

为什么不试试

curl_setopt($curl, CURLOPT_HEADER, 1);

接收标题和内容。您需要做的就是解析$out变量中的标题。

这是Google提取的完整工作示例:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');
header('Content-Type: text/plain; charset=utf-8');

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/');
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$out = curl_exec($curl);
curl_close ($curl);

$out = preg_split('/(\r?\n){2}/', $out, 2);
$headers = $out[0];
$headersArray = preg_split('/\r?\n/', $headers);
$headersArray = array_map(function($h) {
    return preg_split('/:\s{1,}/', $h, 2);
}, $headersArray);

$tmp = [];
foreach($headersArray as $h) {
    $tmp[strtolower($h[0])] = isset($h[1]) ? $h[1] : $h[0];
}
$headersArray = $tmp; $tmp = null;
// $headersArray contains your headers
print_r($headersArray);
?>

这会产生:

Array
(
    [http/1.1 200 ok] => HTTP/1.1 200 OK
    [date] => Thu, 29 Oct 2015 13:26:39 GMT
    [expires] => -1
    [cache-control] => private, max-age=0
    [content-type] => text/html; charset=ISO-8859-1
    [p3p] => CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
    [server] => gws
    [x-xss-protection] => 1; mode=block
    [x-frame-options] => SAMEORIGIN
    [set-cookie] => NID=72=lw6pIMe05MoXu3aykbPi0BR9gZomWqTXBwsk6VG7xtLbLLeWc0I__CLGydE-auttR0G8VulKoZOTrv4eAZovJJi9QyB5hgxBue9pLWcX794Iv6gPlM2QaL9I2t6tjtrADtczAZpHhbnLvjmeDn_AmRj0xKkFPrMhYR84C5lNgzgo1iJpzr5qG2y6xg; expires=Fri, 29-Apr-2016 13:26:39 GMT; path=/; domain=.google.com; HttpOnly
    [alternate-protocol] => 443:quic,p=1
    [alt-svc] => quic="www.google.com:443"; p="1"; ma=600,quic=":443"; p="1"; ma=600
    [accept-ranges] => none
    [vary] => Accept-Encoding
    [transfer-encoding] => chunked
)

从上面的示例中,您需要$headersArray['x-ratelimit-clientremaining'];

希望有所帮助。

编辑:这里有快捷方式(因为您的回复不包含换行符):

$matches = null;
preg_match('/X-RateLimit-ClientRemaining:\s*(\d+)/i', $out, $matches);
echo sprintf('X-RateLimit-ClientRemaining: %u', $matches[1]);

产地:

X-RateLimit-ClientRemaining: 11850
相关问题