Skip to content

Instantly share code, notes, and snippets.

@herdianf
Created October 10, 2023 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herdianf/2f7f5454f2fc582fd5c52c5f01b004ec to your computer and use it in GitHub Desktop.
Save herdianf/2f7f5454f2fc582fd5c52c5f01b004ec to your computer and use it in GitHub Desktop.
PHP curl functions
<?php
/**
* Run a single curl request
*
* @param string $url
* The url for request.
* @param array $options
* Map of options, expected key:
* - method string GET|POST|HEAD
* - headers array eg: ["content-type" => "application/json"];
* - timeout int seconds
* - connect_timeout int seconds
* - useragent string custom user agent.
* - auth ['username' => username, 'password' => password]
*
* @return array
* Return array with this order: [http_status_code, response_body, header_array, errno, error]
*/
function curl($url, $options = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$method = !empty($options['method']) ? strtoupper($options['method']) : 'GET';
$body = $options['body'] ?? '';
$headers = $options['headers'] ?? [];
$timeout = $options['timeout'] ?? 30;
$connect_timeout = $options['connect_timeout'] ?? $timeout;
$cookiejar = $options['cookiejar'] ?? NULL;
$cookiefile = $options['cookiefile'] ?? NULL;
$useragent = $options['useragent'] ?? NULL;
$auth = $options['auth'] ?? [];
if (!empty($cookiejar)) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
}
if (!empty($cookiefile)) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
}
if (!empty($useragent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
}
if (!empty($auth)) {
$user = $auth['username'] ?? NULL;
$password = $auth['password'] ?? NULL;
if ($user && $password) {
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
}
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($method === 'HEAD') {
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
} else if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$headers[] = "content-length: " . strlen($body);
}
$custom_headers = [];
foreach($headers as $k => $v) {
if (is_int($k)) {
$custom_headers[] = $v;
} else {
$custom_headers[] = "$k: $v";
}
}
if (!empty($custom_headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
}
$response = curl_exec($ch);
$response_header = '';
if (empty($response)) {
$response = '';
}
$error = curl_error($ch);
$errno = curl_errno($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
if ($header_size > 0) {
$response_header = substr($response, 0, $header_size);
$response = substr($response, $header_size);
}
curl_close($ch);
$header_out = [];
if (!empty($response_header)) {
$array = explode("\n", $response_header);
foreach($array as $line) {
$line = trim($line);
if (empty($line)) continue;
$keyval = explode(":", $line, 2);
$key = strtolower($keyval[0]);
$val = !empty($keyval[1]) ? trim($keyval[1]) : '';
$header_out[$key] = $val;
}
}
return [$status, $response, $header_out, $errno, $error];
}
// Example usage
[$status, $body] = curl("https://www.example.com/");
// Post request
[$status, $body] = curl("https://httpbin.org/post", [
'method' => 'POST',
'headers' => [
'content-type' => 'application/x-www-form-urlencoded',
],
'body' => http_build_array(['name' => 'Name example', 'email' => 'email@example.com']),
]);
// JSON post request
[$status, $body] = curl("https://httpbin.org/post", [
'method' => 'POST',
'headers' => [
'content-type' => 'application/json',
],
'body' => json_encode(['name' => 'Name example', 'email' => 'email@example.com']),
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment