Last active
November 20, 2019 08:13
-
-
Save coderbyheart/a8a08b5057c4245bb972 to your computer and use it in GitHub Desktop.
Guzzle JSON example
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 | |
// 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
@Morgscode Uh, I don't remember, but I guess the one that was the latest in 2015 (when I created this gist)...