PHP 使用 Guzzle 获得 JSON Web 令牌
PHP get JSON Web Token with Guzzle
我将 Laravel 5.3 与 GuzzleHttp 7 一起使用,并希望对另一台服务器进行 API 调用,以便在 return 中授权并获得 JSON Web 令牌.
curl 命令运行得很好并且 return 有一个 JSON 状态码为 200 的网络令牌:
curl -X POST "https://example.com/api/auth" -H "accept: application/json"
-H "Content-Type: application/json" -d "{ "password": "passwd", "username": "foo"}"
在PHP中:
<?php
namespace App\Policies;
use GuzzleHttp\Client;
class ApiToken
{
// curl -X POST "https://example.com/api/auth" -H "accept: application/json" -H "Content-Type: application/json" -d "{ "password": "passwd", "username": "foo"}"
public function getToken()
{
$username = 'foo';
$password = 'passwd';
$url ='https://example.com/api/auth';
$client = new Client();
try {
$result = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'username' => $username,
'password' => $password,
]
]);
Log::info(print_r($result)); // 1
}
catch (exception $e) { // no exception
if ($e->hasResponse()) {
Log::info(print_r($e->getResponse())); // empty
die();
}
}
}
return $result;
}
$apiToken = new ApiToken;
$apiToken->getToken(); // => GuzzleHttp\Psr7\Response {#3619}
我对您的代码做了一些小改动,使其变得更好,
<?php
namespace App\Policies;
use GuzzleHttp\Client;
class ApiToken
{
// curl -X POST "https://example.com/api/auth" -H "accept: application/json" -H "Content-Type: application/json" -d "{ "password": "passwd", "username": "foo"}"
public function getToken()
{
$username = 'foo';
$password = 'passwd';
$url ='https://example.com/api/auth';
$client = new Client();
try {
$guzzleResponse = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'username' => $username,
'password' => $password,
]
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody()->getContents(), true);
return $response; // or perform your action with $response
// see this answer to know why use getContents()
}
} catch(\GuzzleHttp\Exception\RequestException $e){
// you can catch here 400 response errors and 500 response errors
// You can either use logs here
$error['error'] = $e->getMessage();
$error['request'] = $e->getRequest();
if($e->hasResponse()){
if ($e->getResponse()->getStatusCode() == '400'){
$error['response'] = $e->getResponse();
}
}
Log::info('Error occurred in request.', ['error' => $error]);
} catch(Exception $e){
//other errors
}
}
}
$apiToken = new ApiToken;
$apiToken->getToken();
我将 Laravel 5.3 与 GuzzleHttp 7 一起使用,并希望对另一台服务器进行 API 调用,以便在 return 中授权并获得 JSON Web 令牌.
curl 命令运行得很好并且 return 有一个 JSON 状态码为 200 的网络令牌:
curl -X POST "https://example.com/api/auth" -H "accept: application/json"
-H "Content-Type: application/json" -d "{ "password": "passwd", "username": "foo"}"
在PHP中:
<?php
namespace App\Policies;
use GuzzleHttp\Client;
class ApiToken
{
// curl -X POST "https://example.com/api/auth" -H "accept: application/json" -H "Content-Type: application/json" -d "{ "password": "passwd", "username": "foo"}"
public function getToken()
{
$username = 'foo';
$password = 'passwd';
$url ='https://example.com/api/auth';
$client = new Client();
try {
$result = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'username' => $username,
'password' => $password,
]
]);
Log::info(print_r($result)); // 1
}
catch (exception $e) { // no exception
if ($e->hasResponse()) {
Log::info(print_r($e->getResponse())); // empty
die();
}
}
}
return $result;
}
$apiToken = new ApiToken;
$apiToken->getToken(); // => GuzzleHttp\Psr7\Response {#3619}
我对您的代码做了一些小改动,使其变得更好,
<?php
namespace App\Policies;
use GuzzleHttp\Client;
class ApiToken
{
// curl -X POST "https://example.com/api/auth" -H "accept: application/json" -H "Content-Type: application/json" -d "{ "password": "passwd", "username": "foo"}"
public function getToken()
{
$username = 'foo';
$password = 'passwd';
$url ='https://example.com/api/auth';
$client = new Client();
try {
$guzzleResponse = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'username' => $username,
'password' => $password,
]
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody()->getContents(), true);
return $response; // or perform your action with $response
// see this answer to know why use getContents()
}
} catch(\GuzzleHttp\Exception\RequestException $e){
// you can catch here 400 response errors and 500 response errors
// You can either use logs here
$error['error'] = $e->getMessage();
$error['request'] = $e->getRequest();
if($e->hasResponse()){
if ($e->getResponse()->getStatusCode() == '400'){
$error['response'] = $e->getResponse();
}
}
Log::info('Error occurred in request.', ['error' => $error]);
} catch(Exception $e){
//other errors
}
}
}
$apiToken = new ApiToken;
$apiToken->getToken();