CurlRequest
Making cURL request easy.
The cURL helper class comes with a handy curl function shorthand, that loads and instantiates the helper class automatically and allows you to make cURL request as one-liner commands.
The cURL implementation in PHP has some little known behavioural problems. Sending inputs with the request as an array will automatically change the header content-type to multipart/form-data. While the header can be forced to a different value using the header array, then the actual sending of values will use the multipart/form-data syntax – and that won't work. To get a application/x-www-form-urlencoded header and syntax, the input MUST be sent as a URL-encoded string rather than as an array.
Functions
request
Definition
- Name
- CurlRequest::request()
- Shorthand
- curl()->request()
- Syntax
- Array = curl()->request( String $url [, Array $_options ] );
Description
Make a cURL request using the PHP cURL implementation. cURL can be used to make complex serverside requests, including POST.
Parameters
- $url
-
String The URL to make the request to
- $_options
-
Array Optional array of cURL options
Options
Return values
Array Array containing response values in an Array with these keys:
header
Contains the header information of the response.
body
Contains the response body. This is typically what you are looking for. Equivalent to what you will see in the browser if the request was made directly.
curl_error
Contains any curl error if such occured.
http_code
The http response code of the response – like 200 for success, 404 for not found etc.
last_url
Contains the last accessed url. In some cases the request might be redirected by the receiving server, and the responding url might differ from the requested url. Ie. if your request is redirected to a login page.
cookies
Contains an array of cookies returned with the reponse.
information
Optional index only used when debug option is set. Will contain additional information about response for debugging purposes.
Examples
Plain request
$response = curl()->request("https://janitor.parentnode.dk");
Makes a request to https://janitor.parentnode.dk and returns the response values as an Array.
Request with custom headers
$response = curl()->request("https://janitor.parentnode.dk", [
"headers" => [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic XXX"
]
]);
Makes a request to https://janitor.parentnode.dk, adding two header values to the request, and returns the response values as an Array.
Dependencies
PHP
- curl_init
- curl_setopt
- curl_exec
- curl_error
- curl_getinfo
- is_string
- strtoupper
- strlen
- substr
- fopen
- fclose
Janitor
- None