Skip to content

Instantly share code, notes, and snippets.

@pixeline
Last active May 6, 2022 14:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pixeline/ab9f24b0791dd7b7f735 to your computer and use it in GitHub Desktop.
Save pixeline/ab9f24b0791dd7b7f735 to your computer and use it in GitHub Desktop.
Fetch an external page via Curl. Useful to bypass SAMEORIGIN issues.
<?php
/*
Fetch a quote from http url for a https site (avoids the insecure network error).
usage: this-script.php?url=http://johndoe.com
*/
/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
$url = $_GET['url'];
// Sanitize and validate url
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
die("$url is not a valid URL");
}
$result = get_web_page($url);
echo $result['content'];
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment