CURL 到 Guzzle 有 Header 问题
CURL to Guzzle having Header issue
我正在尝试在 guzzle 上设置授权 header。我的请求已成功使用以下 CURL 代码:
function callApi($url,$accesstoken)
{
$headr = array();
$headr[] = 'Authorization:Bearer '.$accesstoken;
//cURL starts
$crl = curl_init();
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($crl, CURLOPT_HTTPGET,true);
$reply = curl_exec($crl);
//error handling for cURL
if ($reply === false) {
print_r('Curl error: ' . curl_error($crl));
return false;
}
curl_close($crl);
return $reply;
}
我怎么用这个 Guzzle 代码得到 401。我试过设置 header 很多不同的方法,但没有成功。
public function __construct()
{
$this->config = \Config::get('services.******');
$this->client = new Client([
'base_uri' => 'https://*******/' . $this->config['merchant_id']
]);
}
public function getInventoryItems()
{
$response = $this->client->get('items', [
'headers' => [
'Authorization' => 'Bearer' . $this->config['token']
]
]);
return json_decode($response->getBody());
}
'Authorization' => 'Bearer' . $this->config['token']
应该是
'Authorization' => 'Bearer ' . $this->config['token']
我正在尝试在 guzzle 上设置授权 header。我的请求已成功使用以下 CURL 代码:
function callApi($url,$accesstoken)
{
$headr = array();
$headr[] = 'Authorization:Bearer '.$accesstoken;
//cURL starts
$crl = curl_init();
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($crl, CURLOPT_HTTPGET,true);
$reply = curl_exec($crl);
//error handling for cURL
if ($reply === false) {
print_r('Curl error: ' . curl_error($crl));
return false;
}
curl_close($crl);
return $reply;
}
我怎么用这个 Guzzle 代码得到 401。我试过设置 header 很多不同的方法,但没有成功。
public function __construct()
{
$this->config = \Config::get('services.******');
$this->client = new Client([
'base_uri' => 'https://*******/' . $this->config['merchant_id']
]);
}
public function getInventoryItems()
{
$response = $this->client->get('items', [
'headers' => [
'Authorization' => 'Bearer' . $this->config['token']
]
]);
return json_decode($response->getBody());
}
'Authorization' => 'Bearer' . $this->config['token']
应该是
'Authorization' => 'Bearer ' . $this->config['token']