nextstage-brasil / ns-http
This package is abandoned and no longer maintained.
No replacement package was suggested.
Methods and classes for various utilities
1.0.0
2024-09-07 23:12 UTC
Requires
- php: >=7.4
Requires (Dev)
README
vendor/bin/phpunit
The Http
class encapsulates HTTP calls using cURL in PHP. It supports various HTTP methods such as GET, POST, PUT, and DELETE, and allows the configuration of headers, parameters, and SSL options.
Methods
call
Makes an HTTP call to a specified URL.
Parameters
string $url
: The URL to which the request will be made.array $params
: Request parameters (optional, default:[]
).string $method
: HTTP method (GET, POST, PUT, DELETE, etc.) (optional, default: 'GET').array $header
: HTTP headers (optional, default:['Content-Type: application/json']
).bool $ssl
: Verify SSL (optional, default:true
).int $timeout
: Request timeout in seconds (optional, default:30
).
Return
Response
: An object containing the response of the request, including:body
: Response content.error_code
: cURL error code.error
: cURL error message.http_code
: HTTP status code.headers
: Response headers.url
: Effective request URL.
Example Usage
// URL for the request
$url = 'https://api.example.com/data';
// Request parameters
$params = [
'key' => 'value',
'another_param' => 'another_value'
];
// Request headers
$headers = [
'Content-Type: application/json',
'Authorization: Bearer your_token_here'
];
$response = Http::call($url, $params, 'GET', $headers);
// Accessing the response
echo $response->getBody(); // Response content
echo $response->getBodyAsObject(); // Response content as JSON Object
echo $response->getBodyAsArray(); // Response content as JSON Array
echo $response->getHttpCode(); // HTTP status code