使用 Guzzle 客户端获取 Api 数据
Get Api Data using Guzzle client
我正在尝试从 API 调用数据,它在 Postman 和 jquery 中工作正常,它有一个必须发送的 API 键名“APP_KEY”作为 header 否则无法访问 API 的数据,我正在尝试使用 Guzzle HTTP 客户端获取数据但它没有发送 header,
这里是需要传入的Header:
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());
请告诉我如何将 header 和 Link 发送到 API
任何帮助将不胜感激
这里是邮递员ss
你也应该使用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
} catch(Exception $e){
//other errors
}
}
就这么简单,更多信息,请看docs
我正在尝试从 API 调用数据,它在 Postman 和 jquery 中工作正常,它有一个必须发送的 API 键名“APP_KEY”作为 header 否则无法访问 API 的数据,我正在尝试使用 Guzzle HTTP 客户端获取数据但它没有发送 header,
这里是需要传入的Header:
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());
请告诉我如何将 header 和 Link 发送到 API
任何帮助将不胜感激
这里是邮递员ss
你也应该使用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
} catch(Exception $e){
//other errors
}
}
就这么简单,更多信息,请看docs