Skip to content

Instantly share code, notes, and snippets.

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 richard457/e689bf6f719a07b15fbb84ea0c63128a to your computer and use it in GitHub Desktop.
Save richard457/e689bf6f719a07b15fbb84ea0c63128a to your computer and use it in GitHub Desktop.
Guzzle JSON example
<?php
// GET
$request = $client->get($url, array('Accept' => 'application/json'));
$response = $request->send();
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
// Error
}
// POST with basic auth
$headers = [
'Content-type' => 'application/json; charset=utf-8',
'Accept' => 'application/json',
'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
];
$response = $client->post($url, $headers, json_encode($data))->send();
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
// Error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment