Client::get
Send a GET request
Description
public array Client::get( string $url [, array $headers = array() ] )
Returns array('status' => int, 'body' => string). A response with an error status is returned normally; a transport failure throws Fegg\Http\ClientException (see Errors).
Arguments
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $url | string | http:// or https:// URL, with its query string | |
| $headers | array | array() | Request headers, each as 'Name: value' |
Example
Controller
use Fegg\Http\Client;
$query = http_build_query(array('category' => 2, 'q' => $this->in('q')));
$response = (new Client())->get('https://api.example.com/items?' . $query, array(
'Accept: application/json',
'Authorization: Bearer ' . feggEnv('ITEMS_API_TOKEN'),
));
if ($response['status'] === 200) {
$items = json_decode($response['body'], true);
}
Keep tokens out of the code: read them with feggEnv() from env.php (see Settings).