场景
- 接口设置了tymondesigns/jwt-auth 认证,
所以需要在请求的头部放置authorization 信息
解决
<?php
$authorization = 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sZWFybi5jYXJzb25saXVzLnZpcFwvYXBpXC9hdXRoXC9sb2dpbiIsImlhdCI6MTUzODg4MDk2NSwiZXhwIjoxNTM4ODg0NTY1LCJuYmYiOjE1Mzg4ODA5NjUsImp0aSI6Im10Rm1YQUMyQldjR2FhTVIiLCJzdWIiOjEsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.xZGnrbo8fDGQ8OstGhaDlsEaPP-00sHumwUpsrA-zdw2';
$url
curlAuth::curlAuth($url, $authorization);
class curlAuth
{
private $authorization = [];
private $url;
private function __construct(string $url, string $authorization)
{
$this->url = $url;
array_push($this->authorization, $authorization);
}
public static function curlAuth(string $url, string $authorization): array
{
return (new static($url, $authorization))->curlRequest();
}
private function curlRequest(): array
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->authorization);
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
if (is_string($data)) {
return json_decode($data, true);
}
return $data;
}
}