Last active
February 25, 2020 13:43
-
-
Save marcocesarato/3063e428aee328260709e5ff65527cff to your computer and use it in GitHub Desktop.
Http request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* HTTP Request | |
* @author Marco Cesarato <cesarato.developer@gmail.com> | |
* @param $url | |
* @return mixed | |
*/ | |
function http_request($url, $body = false, $method = "GET", $headers = array(), $timeout = 15) { | |
$options = array( | |
CURLOPT_RETURNTRANSFER => true, // return web page | |
CURLOPT_HEADER => true, // return headers in addition to content | |
CURLOPT_FOLLOWLOCATION => true, // follow redirects | |
CURLOPT_ENCODING => "", // handle all encodings | |
CURLOPT_AUTOREFERER => true, // set referer on redirect | |
CURLOPT_CONNECTTIMEOUT => $timeout, // timeout on connect | |
CURLOPT_TIMEOUT => $timeout, // timeout on response | |
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects | |
CURLINFO_HEADER_OUT => true, | |
CURLOPT_SSL_VERIFYPEER => false, // Validate SSL Cert | |
CURLOPT_SSL_VERIFYHOST => false, // Validate SSL Cert | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_HTTPHEADER => array( | |
"Accept-Language: " . @$_SERVER['HTTP_ACCEPT_LANGUAGE'], | |
"Cache-Control: no-cache" | |
), | |
CURLOPT_USERAGENT => @$_SERVER['HTTP_USER_AGENT'], | |
CURLOPT_POSTFIELDS => (empty($body) ? null : $body), | |
); | |
if (!empty($headers)) { | |
$options[CURLOPT_HTTPHEADER] = $headers; | |
} | |
if ($body !== false && !empty($body) && $method === "GET") { | |
$containData = (strpos($url, '?') !== false); | |
$url = $url . ($containData ? '&' : '?') . http_build_query($body); | |
} | |
$method = strtoupper($method); | |
if(in_array($method, array('POST', 'PUT', 'PATCH', 'DELETE'))) { | |
$options[CURLOPT_CUSTOMREQUEST] = $method; | |
} | |
$options = array_filter($options); | |
$ch = curl_init($url); | |
$rough_content = http_exec_request($ch, $options); | |
$err = curl_errno($ch); | |
$errmsg = curl_error($ch); | |
$header = curl_getinfo($ch); | |
curl_close($ch); | |
$header_content = substr($rough_content, 0, $header['header_size']); | |
$body_content = trim(str_replace($header_content, '', $rough_content)); | |
$header['errno'] = $err; | |
$header['errmsg'] = $errmsg; | |
$header['headers'] = $header_content; | |
$header['content'] = $body_content; | |
return $header; | |
} | |
/** | |
* Prevent 301/302/307 Code | |
* @param resource $ch | |
* @param array $options | |
* @return bool|string | |
*/ | |
function http_exec_request($ch, $options = array()) { | |
$options[CURLOPT_FOLLOWLOCATION] = false; | |
curl_setopt_array($ch, $options); | |
$rough_content = curl_exec($ch); | |
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if($http_code == 301 || $http_code == 302 || $http_code == 307) { | |
preg_match('/(Location:|URI:)(.*?)\n/', $rough_content, $matches); | |
if(isset($matches[2])) { | |
$redirect_url = trim($matches[2]); | |
if($redirect_url !== '') { | |
$options[CURLOPT_URL] = $redirect_url; | |
return http_exec_request($ch, $options); | |
} | |
} | |
} | |
return $rough_content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment