To perform requests via guzzlehttp the following steps are necessary:
•Process response (read requested data from response, handle error)
Scheme
// Initialize client
$client = new Client(
[
'base_uri' => 'http://example.org/jobrouter/api/rest/v2/',
'cookies' => true
]
);
try {
// Send request
$response = $client->get(
'http://example.org/jobrouter/api/rest/v2/path/to/some/get/route'
);
$body = $response->getBody();
// Read further information from the response ...
} catch (Exception $e) {
echo "Error during performance: " . $e->getMessage();
}
To perform the request guzzlehttp offers the class Client . When initializing the client several options can be determined:
•base_uri – API URL of the server. Insert here the URL for the JobRouter REST API. When sending requests the URL of the requested resource is indicated relatively to this basis URL.
•http_errors – This option is activated by default (true). So responses with HTTPerror codes (HTTP status code between 400 and 500) are returned as exceptions. Deactivate the option, if you want to handle the error messages of the responses yourself.
•verify – This option is activated by default (true). So the server is verified through its certificates, when a request is made (SSL verification). If you are using JobRouter with HTTPS protocol, this option should be placed in PEM format on the path to the certification data of the server ('verify' => '/path/to/my/certificate/cert.pem'). Alternatively, you can insert the path in the php.ini as openssl.cafile . Please only deactivate the option, if the request is send to an internal test system.
•cookies – This option does not contain default values. When setting truea cookie session is used for the client and subsequent requests.
•sink – Determines, where and how the response to a request is stored (e.g. as file in the file system). This option is used for downloading files.
The options can't be changed any more after initializing the client. Information on further options can be found in the guzzlehttp documentation.
For sending the HTTP request the method request() can be used. Additionally, the HTTP methods can be used as method-aliases, e.g.:
•$client->get('<Ressource-URL>') for GET routes,
•$client->post('<Ressource-URL>', $postData) for POST routes, etc.
If the request requires additional data, these are send in the calling method.
The request methods returns a response object. The following methods are available for this object:
•getStatusCode(): determine HTTP status code of the response
•getBody(): Determine the data returned in the response
If you defined the client in such a way that no exceptions are displayed in case of errors (http_errors => false), you can read the error report from the errors array in the body of the response.
$client = new Client(
[
'base_uri' => 'http://example.org/jobrouter/api/rest/v2/',
'cookies' => true,
'http_errors' => false,
]
);
// Authenticate non-existing user
$response = $client->post('application/sessions', [
'json' => [
'username' => 'nonexisting',
'password' => 'mypassword',
],
]);
// Determine error message
echo "Code: " . $response->getStatusCode() . "\n";
$responseData = json_decode($response->getBody(), true);
$errors = $responseData['errors'];
foreach($errors as $error) {
echo $error[0] . "\n";
}