Skip to content

Instantly share code, notes, and snippets.

@QROkes
Created March 18, 2016 18:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QROkes/99fc55b7e4e4080f0c3c to your computer and use it in GitHub Desktop.
Save QROkes/99fc55b7e4e4080f0c3c to your computer and use it in GitHub Desktop.
Get HTML source code with cUrl (file_get_contents as fallback)
$urlcontent = qr_loadUrl( 'http://myurl.com' );
function qr_loadUrl( $url ) {
if(is_callable( 'curl_init' )) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
}
if( empty($data) || !is_callable('curl_init') ) {
$opts = array('http'=>array('header' => 'Connection: close'));
$context = stream_context_create($opts);
$headers = get_headers($url);
$httpcode = substr($headers[0], 9, 3);
if( $httpcode == '200' )
$data = file_get_contents($url, false, $context);
else{
$data = '{"div":"Error ' . $httpcode . ': Invalid Url<br />"}';
}
}
return $data;
}
@majidpeidaei
Copy link

hi
i cant read https site by this code.

@dansp89
Copy link

dansp89 commented Mar 1, 2022

Use:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment