Skip to content

Instantly share code, notes, and snippets.

@pixelbrackets
Last active May 15, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbrackets/696ceb990d9d4e379e9ff199eaa607e2 to your computer and use it in GitHub Desktop.
Save pixelbrackets/696ceb990d9d4e379e9ff199eaa607e2 to your computer and use it in GitHub Desktop.
Guzzle JSON Request Debugging
<?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