Forked from roberto-butti/send_json_post_auth_with_guzzle.php
Created
May 26, 2020 10:48
-
-
Save ilearnbydoing/3289f6e457ca804fe9b36b7a773a7324 to your computer and use it in GitHub Desktop.
Send a JSON in a POST, with HTTP Basic Auth (Guzzle Version)
This file contains hidden or 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 | |
$endpoint_url="your_url_here"; | |
$string_json = "your_json_string"; | |
$username="username"; | |
$password ="password"; | |
$client = new Client(); | |
$options= array( | |
'auth' => [ | |
$username, | |
$password | |
], | |
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'], | |
'body' => $string_json, | |
"debug" => true | |
); | |
try { | |
$res = $client->post($endpoint_url, $options); | |
} catch (ClientException $e) { | |
echo $e->getRequest() . "\n"; | |
if ($e->hasResponse()) { | |
echo $e->getResponse() . "\n"; | |
} | |
} | |
echo "OO<h1>".$res->getStatusCode()."</h1>OO"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment