我正在尝试从API中调用数据,它在Postman和jquery中工作得很好,它有一个API键名为"APP_KEY“,必须作为头部发送,否则API的数据无法访问,我试图使用Guzzle客户端获取数据,但它没有发送头部。
下面是需要传入的头部:
APP_KEY=>QAWLhIK2p5下面是控制器部分:
$client = new Client();
$body['headers']= array('APP_KEY'=>'QAWLhIK2p5');
$response = $client->GET('http://localhost:1080/busy/public/api/material',$body);
//dd($response->getStatusCode());
print_r($data = $response->getResponse()->getContents());请告诉我如何发送带有指向API的链接的报头
如有任何帮助,将不胜感激
邮递员来了。

发布于 2020-10-10 00:50:31
你也应该使用guzzle作为标签,我会在那天回答,你需要改变你的代码,
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
public function yourFunction()
{
try {
$client = new Client();
$guzzleResponse = $client->get(
'http://localhost:1080/busy/public/api/material', [
'headers' => [
'APP_KEY'=>'QAWLhIK2p5'
],
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody(),true);
}
} catch (RequestException $e) {
// you can catch here 400 response errors and 500 response errors
// see this https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600
} catch(Exception $e){
//other errors
}
}就这么简单,想要了解更多信息,只需查看docs
https://stackoverflow.com/questions/64216262
复制相似问题