Skip to main content
Version: Fegg 5.x

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​

NameData typeDefaultRemarks
$urlstringhttp:// or https:// URL, with its query string
$headersarrayarray()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).