Basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
use GuzzleHttp\Client; $response = $client->request('POST', 'http://httpbin.org/post', [ 'form_params' => [ 'field_name' => 'abc', 'other_field' => '123', 'nested_field' => [ 'nested' => 'hello' ] ] ]); $response = $client->request('POST', 'http://httpbin.org/post', [ 'json' => [ 'field_name' => 'abc', 'other_field' => '123', 'nested_field' => [ 'nested' => 'hello' ] ] ]); $res = $this->client->request( 'GET', 'members/' . $phone . '/prepaidcards', [ 'query' => [ 'phone' => $phone, ] ]); |
Get raw response
1 2 3 4 5 |
//Return all data $contents = (string) $response->getBody(); //getContents returns the remaining contents $contents = $response->getBody()->getContents(); |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
use Psr\Http\Message\ResponseInterface; use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\StreamInterface; class HttpTool { public $err = ''; public function __construct(){ } public function get($url, $data) { $client = new \GuzzleHttp\Client(); $response = $client->request('GET', $url, $data); //logg($response->getStatusCode()); if($response->getStatusCode() == 200){ $res = json_decode($response->getBody()); return $res; }else{ $this->err = (string)$response->getBody(); return false; } } } |
Reference: