FastSitePHP\Net\HttpResponse

This class is returned from HTTP Request functions when using the [HttpClient] class.

Source Code

GitHub

Exemple de Code

Using the HTTP Client

// The HttpClient can be used to simplify communication with other Web Services,
// HTTP API’s, and works great for calling and returning the result of local
// services – for example an AI/ML (Artificial Intelligence / Machine Learning)
// Service written in Python with TensorFlow or scikit-learn.

// Perform a simple HTTP GET Request and check the result
$res = \FastSitePHP\Net\HttpClient::get($url);
if ($res->error) {
    // An error would be returned in the event of a major failure such as
    // a timeout or SSL Cert Error. A 404 or 500 Response from the server
    // would be handled by checking the [status_code].
    $error = $res->error;
} else {
    $status_code = $res->status_code; // 200, 404, 500, etc
    $headers = $res->headers; // Array of Response Headers
    $content = $res->content; // Response Content as a String - HTML, Text, etc
    $info = $res->info; // Array of Info such as Time Stats
}

// Perform an HTTP GET Request and read the JSON Result. If the Response
// Content-Type is 'application/json' then [$res->json] will contain an array
// otherwise null. Request Headers can be passed an optional paramater.
$headers = [
    'X-API-Key' => 'ab82050cf5907934fa1d0f6f66284642a01d1ba2280656870c',
    'X-Custom-Header' => 'Test',
];
$res_json = \FastSitePHP\Net\HttpClient::get($url, $headers);
$json = $res->json;
$text = $res->content;

// Submit a HTTP POST Request as JSON and also as a Form.
// Data can be either an Array or Object and Headers are optional.
$data = [
    'text' => 'test',
    'num' => 123,
];
$res_post = \FastSitePHP\Net\HttpClient::postJson($url, $data, $headers);
$res_form = \FastSitePHP\Net\HttpClient::postForm($url, $data);

// When using PHP 5.5 or later 'multipart/form-data' Form Posts are supported
// with the PHP built-in class [CURLFile]:
/*
$data = [
    'field1' => 'test',
    'file' => new \CURLFile($file_path),
];
*/

// Save the Response Content as a File Download
// Just like [postJson()] and [postForm()] Request Headers are optional.
$res_file = \FastSitePHP\Net\HttpClient::downloadFile($url, $save_path, $headers);
$saved_path = $res_file->content;

// The above code demo shows the 4 helper static functions [get(), postJson(),
// postForm(), and downloadFile()], additional options are available when using
// the HttpClient as an object with the [request()] method.

// Submit a PUT Request with a file as the Request Body
$http = new \FastSitePHP\Net\HttpClient();
$res_put = $http->request($url, [
    'method' => 'PUT',
    'headers' => $headers,
    'send_file' => $file_path,
]);

Properties

Name Data Type Default Description
error int
string
0 Error code if there is a major failure with an HTTP Request such as a timeout or SSL Cert Error.  0 if no error.

The error code is the value returned by [curl_errno()].
status_code int
null
null Status Code of the Response [200, 404, 500, etc].
headers array
null
null HTTP Response Headers
content string
false
null
null Response Body as a Text String. PHP Strings are array's of bytes so binary responses will also be in string format.
json array
null
null Response Body parsed to an Array for JSON Responses. This can be turned off by setting the option [parse_json = false] from [HttpClient->request()].
info array
null
null Array of detailed information for the request and response from [curl_getinfo()]. Example ($res->info['CURLINFO_TOTAL_TIME']) if set will return the total transaction time in seconds.