Created
June 18, 2020 17:07
-
-
Save PhongGCS/f4b1487bd66e015ac22ae5fe7bd99cd2 to your computer and use it in GitHub Desktop.
This file contains 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
**GET Request using Guzzle** | |
If you look at the REQ|RES website they provided a few endpoints for ‘GET’ request. I will take the example of ‘LIST USERS’ endpoint. For getting users list, they ask to send a GET request to this endpoint | |
https://reqres.in/api/users?page=2. | |
<?php | |
require_once "vendor/autoload.php"; | |
use GuzzleHttp\Client; | |
$client = new Client([ | |
// Base URI is used with relative requests | |
'base_uri' => 'https://reqres.in', | |
]); | |
$response = $client->request('GET', '/api/users', [ | |
'query' => [ | |
'page' => '2', | |
] | |
]); | |
//get status code using $response->getStatusCode(); | |
$body = $response->getBody(); | |
$arr_body = json_decode($body); | |
print_r($arr_body); | |
---------------- ---------------- ---------------- ---------------- ---------------- ---------------- | |
**POST Request using Guzzle** | |
Probably, there are 2 types of POST requests. You may need to POST parameters as application/x-www-form-urlencoded POST request or upload JSON encoded data as a body of the request. | |
You can post JSON encoded data as shown below. | |
<?php | |
require_once "vendor/autoload.php"; | |
use GuzzleHttp\Client; | |
$client = new Client([ | |
// Base URI is used with relative requests | |
'base_uri' => 'https://reqres.in', | |
]); | |
$response = $client->request('POST', '/api/users', [ | |
'json' => [ | |
'name' => 'Sam', | |
'job' => 'Developer' | |
] | |
]); | |
//get status code using $response->getStatusCode(); | |
$body = $response->getBody(); | |
$arr_body = json_decode($body); | |
print_r($arr_body); | |
** POST application/x-www-form-urlencoded POST request ** | |
<?php | |
require_once "vendor/autoload.php"; | |
use GuzzleHttp\Client; | |
$client = new Client([ | |
// Base URI is used with relative requests | |
'base_uri' => 'BASE_URL_ENDPOINT', | |
]); | |
$client->request('POST', '/endpoint_here', [ | |
'form_params' => [ | |
'foo' => 'bar', | |
'baz' => ['hi', 'there!'] | |
] | |
]); | |
**Send Authorization token in each HTTP request** | |
<?php | |
$client->request('POST', '/endpoint_here', [ | |
"headers" => [ | |
"Authorization" => "Bearer TOKEN_VALUE" | |
], | |
'form_params' => [ | |
'foo' => 'bar', | |
'baz' => ['hi', 'there!'] | |
] | |
]); | |
** Copy File from Remote Server using Guzzle ** | |
<?php | |
require_once "vendor/autoload.php"; | |
use GuzzleHttp\Client; | |
$fp = fopen('blog.jpg', 'wb'); | |
$client = new \GuzzleHttp\Client(); | |
$request = $client->get('https://artisansweb.net/wp-content/uploads/2020/03/blog.jpg', ['sink' => $fp]); | |
fclose($fp); | |
**File upload using Guzzle** | |
<?php | |
require_once "vendor/autoload.php"; | |
use GuzzleHttp\Client; | |
$client = new Client([ | |
// Base URI is used with relative requests | |
'base_uri' => 'BASE_URL_HERE', | |
]); | |
$client->request('POST', '/endpoint_here', [ | |
'multipart' => [ | |
[ | |
'name' => 'files', // name value requires by endpoint | |
'contents' => fopen('/path/to/file', 'r'), | |
'filename' => 'custom_image.jpg' | |
], | |
] | |
]); | |
** Live example of reSmush.it API which sends an optimized version of the image in response. ** | |
<?php | |
require_once "vendor/autoload.php"; | |
use GuzzleHttp\Client; | |
try { | |
$client = new Client([ | |
// Base URI is used with relative requests | |
'base_uri' => 'http://api.resmush.it', | |
]); | |
$response = $client->request('POST', "?qlty=92", [ | |
'multipart' => [ | |
[ | |
'name' => 'files', // name value requires by endpoint | |
'contents' => fopen(getcwd().'/blog.jpg', 'r'), | |
'filename' => 'blog.jpg', | |
'headers' => array('Content-Type' => mime_content_type(getcwd().'/blog.jpg')) | |
] | |
] | |
]); | |
if (200 == $response->getStatusCode()) { | |
$response = $response->getBody(); | |
$arr_result = json_decode($response); | |
print_r($arr_result); | |
} | |
} catch (\Exception $e) { | |
echo $e->getMessage(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment