Last active
May 15, 2020 15:21
-
-
Save pixelbrackets/696ceb990d9d4e379e9ff199eaa607e2 to your computer and use it in GitHub Desktop.
Guzzle JSON Request Debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// composer require guzzlehttp/guzzle | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
error_reporting(E_ALL); | |
require __DIR__ . '/vendor/autoload.php'; | |
$requestUrl = 'https://httpbin.org/post'; | |
$requestToken = 'jso6TtMOrvRithiqnwMUmIB11d3Uh53uYIsDlob4eS'; | |
$requestBody = [ | |
'foo' => | |
[ | |
'text' => 'Hello World', | |
'icon' => '1749' | |
], | |
]; | |
$client = new GuzzleHttp\Client(); | |
try { | |
$response = $client->request( | |
'POST', | |
$requestUrl, | |
[ | |
'headers' => [ | |
'X-Access-Token' => $requestToken, | |
], | |
'json' => $requestBody, // sets required JSON headers automatically | |
//'debug' => 1 | |
] | |
); | |
$data = json_decode((string) $response->getBody(), true); // true = array instead of object | |
print_r($data); | |
} catch (GuzzleHttp\Exception\RequestException $exception) { | |
echo GuzzleHttp\Psr7\str($exception->getRequest()); | |
if ($exception->hasResponse()) { | |
echo GuzzleHttp\Psr7\str($exception->getResponse()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment