Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Last active November 20, 2019 08:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save coderbyheart/a8a08b5057c4245bb972 to your computer and use it in GitHub Desktop.
Save coderbyheart/a8a08b5057c4245bb972 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
}
@Morgscode
Copy link

Morgscode commented Sep 29, 2019

Can you just confirm which Guzzle version this is for? (:

tia

@coderbyheart
Copy link
Author

@Morgscode Uh, I don't remember, but I guess the one that was the latest in 2015 (when I created this gist)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment