Skip to content

Instantly share code, notes, and snippets.

@abdoulmouctard
Last active December 16, 2020 09:06
Show Gist options
  • Save abdoulmouctard/7f5b09470592f9463c9666cddf877d73 to your computer and use it in GitHub Desktop.
Save abdoulmouctard/7f5b09470592f9463c9666cddf877d73 to your computer and use it in GitHub Desktop.
Secure Fetch with Curl in PHP
<?php
function get_web_page(string $url, string $username, string $password)
{
$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
);
$curl = curl_init($url);
curl_setopt_array($curl, $options);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
$content = curl_exec($curl);
$err = curl_errno($curl);
$errmsg = curl_error($curl);
$header = curl_getinfo($curl);
curl_close($curl);
return [
"header" => $header,
"content" => $content,
"errors" => [$err, "message" => $errmsg]
];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment