Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ilearnbydoing/3289f6e457ca804fe9b36b7a773a7324 to your computer and use it in GitHub Desktop.
Save ilearnbydoing/3289f6e457ca804fe9b36b7a773a7324 to your computer and use it in GitHub Desktop.
Send a JSON in a POST, with HTTP Basic Auth (Guzzle Version)
<?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