client::__construct()
Create a client request object, which can be further customized and ultimately sent to the specified URL.
Prototype
client __construct(string $url, array $options = [])
Parameters
- url - URL to send request to.
- options - Optional associative array with any of the following key/value pairs.
- auth - Associative array of authorization options with the following key/value.
- type - Currently
basic
orbearer
are recognized. - token - Must be provided when type is 'bearer'.
- username - Must be provided when type is 'basic'.
- password - Must be provided when type is 'basic'.
- type - Currently
- body - The main body of the request as a string, array or stdObject. A string is useful for sending plain text or already encoded parameters because a string will be passed directly without any further processing. An array or stdObject will be encoded as JSON and the Content-Type header will be set to 'application/json'.
- boundary - String to use as the boundary string when the request is multipart/form-data. If not specified, a randomly generated 32 character string will be used.
- debug - If TRUE, additional debug information will be logged to the event log.
- encode - How to encode the fields. Recognized encodings are:
'auto'
- The default.'application/x-www-form-urlencoded'
- URL encode post or get values.'multipart/form-data'
- To post as multipart form data.
- fields - Associative array of key/value pairs of fields to encode in the HTTP request. If a string is provided, it is treated like the body option.
- Files - One of the following object formats can be used to represent a file to encode in a POST multipart/form-data request as a field value. The filetype is optional in all cases and will be based on the value if not provided. The filename from the end of the value will be used if a filename is not provided for a url type.
(object)['type'=>'string', 'filetype'=>'application/pdf', 'filename'=>'file.pdf', 'value'=>'bytes']
(object)['type'=>'base64', 'filetype'=>'application/pdf', 'filename'=>'file.pdf', 'value'=>'encoded bytes']
(object)['type'=>'url', 'filetype'=>'application/pdf', 'filename'=>'file.pdf', 'value'=>'path/to/file.pdf']
- Files - One of the following object formats can be used to represent a file to encode in a POST multipart/form-data request as a field value. The filetype is optional in all cases and will be based on the value if not provided. The filename from the end of the value will be used if a filename is not provided for a url type.
- follow_location - TRUE (default) to follow redirects to their final location. FALSE to return the first HTTP response, even if it is a redirect. For more information, see CURLOPT_FOLLOWLOCATION.
- headers - Array of strings where each string is an HTTP header in the form of 'Header-Name: Header-Value', or an associative array where the key is Header-Name and the value is Header-Value.
- log - Log where errors, warnings, notices and other events should be logged.
- method - 'get' (default), 'post', 'put', 'delete' or others.
- resolve - Provide a custom IP address for a specific host and port pair. An array of hostname, port, and IP address strings, each element separated by a colon. In the format: array("example.com:80:127.0.0.1"). When an array of strings is specified, hostnames for specific ports will resolve to the specified IP addresses, instead of using the usual DNS resolution process.
- ssl_verifyhost - TRUE (default) to ensure the SSL certificate for the requested URL includes a matching hostname. FALSE to ignore this check. For more information, see CURLOPT_SSL_VERIFYHOST.
- ssl_verifypeer - TRUE (default) to ensure the SSL certificate can be trusted through a recognized certificate authority. For more information, see CURLOPT_SSL_VERIFYPEER.
- timeout - Number of seconds to wait for a response before returning with a timeout error. Default is 60. If there is a timeout, the response error property will be set, but info will also be populated with applicable values, including total_time (typically just slightly longer than the timeout period).
- verify_content_type - TRUE (default) will verify the response body contents matches the response Content-Type header format. In particular, if application/json is specified, the body content will be decoded and a warning emitted if it is not valid. FALSE will not emit a warning even if the body content and Content-Type don't match.
- auth - Associative array of authorization options with the following key/value.
Example
$url = 'https://demo.webcomand.com/ws/get';
$token = 'authentication-token-here';
$request = new \io_comand_web\client($url, [
'method' => 'post'
]);
$request->headers = [
"Content-Type: application/json",
"Authorization: Token $token"
];
$request->body = '{"query": "SELECT OID, Title FROM HelpDoc"}';
$response = $request->send();
if(!$response || !isset($response->info)) {
echo("Unexpected result.\n");
}
echo("Response Payload: " . $response->payload . "\n");
echo("Response HTTP Code: " . $response->info['http_code']);
// if the response was JSON
if(isset($response->data)) {
echo("JSON Data: " . print_r($response->data, TRUE));
}