Jump to content

cURL returning blank page


soma56

Recommended Posts

I'm playing around with cURL and not having much success. No errors, but rather - nothing at all. I found this simple cURL script that I've been working with in attempt to understand cURL better.

 

<?php
$Url = "http://www.mysimplehtmlsite.ca";

function DownloadUrl($Url){

    // create a new curl resource
    $ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 1); 

    // set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // set referer:
    curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");

    // user agent:
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

    // remove header? 0 = yes, 1 = no
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // should curl return or print the data? true = return, false = print
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);

    // download the given URL, and return output
    $output = curl_exec($ch);

// close the curl resource, and free system resources
    curl_close($ch);

    // print output
    return $output;

}

DownloadUrl($Url);
?>

 

After some reading I did find a couple of potential solutions:

 

Someone suggested to ensure that cURL was installed - didn't work

    // is curl installed?
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }

 

I read somewhere else that return the output before closing the cURL resource? - didn't work

 

    // print output
    return $output;

    // close the curl resource, and free system resources
    curl_close($ch);

 

I got desperate so I tried some illogical things - didn't work

    // print output
    return $output;
echo $output;
print $output;

Finally I found a suggestion on checking to see if an error occurred - didn't work (no errors came back)

// Check if any error occured
if(!curl_errno($ch))
{
 $info = curl_getinfo($ch);

	echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

 

I suspect this is a very simple fix if anyone has a second and can help me out.

Link to comment
https://forums.phpfreaks.com/topic/205015-curl-returning-blank-page/
Share on other sites

You're returning the $output at the end of your function, but you're not doing anything with it.

 

Try:

<?php
$Url = "http://www.mysimplehtmlsite.ca";

function DownloadUrl($Url){

// create a new curl resource
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 1);

// set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);

// set referer:
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");

// user agent:
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

// remove header? 0 = yes, 1 = no
curl_setopt($ch, CURLOPT_HEADER, 0);

// should curl return or print the data? true = return, false = print
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 50);

// download the given URL, and return output
$output = curl_exec($ch);

// close the curl resource, and free system resources
curl_close($ch);

// print output
return $output;

}

$page = DownloadUrl($Url);
echo $page;
?>

 

Ken

Thanks Ken:)

 

I decided to play with other scripts. Out of curiosity does anyone know what I'm missing here? It seems to parse however simply brings back 'Array':

 

$url = "http://www.mysite.ca";

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_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
    );

    $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;
}

$contents = get_web_page($url);
echo $contents;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.