gpt4 book ai didi

php - 获取 cURL 响应 header 和响应正文

转载 作者:行者123 更新时间:2023-12-02 01:32:32 24 4
gpt4 key购买 nike

我正在使用 Imgur API 上传图片。他们在 API docs 中有详细说明每个请求(当我通过他们的 API 上传图片时)也有响应 header ,它会告诉我帐户还剩多少信用。

我需要返回 HTTP 响应 header 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 来 self 没有包含的循环)

最佳答案

为什么不试试

curl_setopt($curl, CURLOPT_HEADER, 1);

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

这是从 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

关于php - 获取 cURL 响应 header 和响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com