Last active
December 28, 2015 07:09
-
-
Save m-manu/7462652 to your computer and use it in GitHub Desktop.
Read entire contents of a URL into one string. Allows you to pass parameters or raw data. Supports GET/POST. More convenient and feature-rich compared to file_get_contents if you're using it on URLs!
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 | |
/** | |
* Read entire contents of given URL as a string. Optionally, get headers as well in an easily accessible array. | |
* @param string $url URL to fetch data from | |
* @param mixed $params Parameters to the URL, typically an array. In case of POST, can be a string | |
* @param string $method HTTP method - can be 'GET' (default) or 'POST' | |
* @param bool $include_headers Should response headers be included? | |
* @return mixed Contents of the webpage as a string. Returns <i>false</i> in case of failure. If parameter <b>$include_headers</b> is <i>true</i>, returns an <i>array</i> containing response code, response headers and response body. | |
*/ | |
function url_get_contents($url, $params = null, $method = 'GET', $include_headers = false) { | |
$contents = false; | |
if (!in_array($method, array('GET', 'POST'))) { | |
error_log(__FUNCTION__ . ": Unknown method '$method'"); | |
return false; | |
} | |
if ($method == 'GET') { | |
if (is_array($params) && count($params) > 0) { | |
if ($params === array_values($params)) { | |
error_log(__FUNCTION__ . ": Numerical array recieved for argument '\$params' (assoc array expected)"); | |
return false; | |
} | |
else { | |
$url .= '?' . http_build_query($params); | |
} | |
} | |
elseif (!is_null($params)) { | |
error_log(__FUNCTION__ . ": If you're making a GET request, argument \$params must be null or assoc array."); | |
return false; | |
} | |
} | |
$ch = curl_init($url); | |
if ($ch !== false) { | |
curl_setopt_array($ch, array( | |
CURLOPT_HEADER => (bool) $include_headers, | |
CURLOPT_RETURNTRANSFER => true, | |
)); | |
if ($method == 'POST') { | |
curl_setopt($ch, CURLOPT_POST, true); | |
if (is_string($params) || is_array($params)) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
} | |
else { | |
error_log(__FUNCTION__ . ": Argument \$params should be an array of parameters or (if you want to send raw data) a string"); | |
return false; | |
} | |
} | |
$rawcontents = curl_exec($ch); | |
if ($include_headers === true) { | |
$contents = array(); | |
$contents['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$headers_str = substr($rawcontents, 0, $header_size); | |
$rawheaders = explode("\n", $headers_str); | |
$contents['headers'] = array(); | |
for ($i = 1; $i < count($rawheaders) - 2; $i++) { | |
list($header_name, $header_value) = explode(':', $rawheaders[$i], 2); | |
$header_name_lowercase = strtolower($header_name); | |
if (isset($contents['headers'][$header_name_lowercase])) { | |
if (is_array($contents['headers'][$header_name_lowercase])) { | |
$contents['headers'][$header_name_lowercase][] = trim($header_value); | |
} | |
else { | |
$prev_header_value = $contents['headers'][$header_name_lowercase]; | |
$contents['headers'][$header_name_lowercase] = array($prev_header_value, trim($header_value)); | |
} | |
} | |
else { | |
$contents['headers'][$header_name_lowercase] = trim($header_value); | |
} | |
} | |
$contents['body'] = substr($rawcontents, $header_size); | |
if ($contents['body'] === false) { | |
$contents['body'] = ""; | |
} | |
} | |
else { | |
$contents = $rawcontents; | |
} | |
curl_close($ch); | |
} | |
return $contents; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment