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
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

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

You're returning an array from the function and you can not just echo an array. You can do

<?php
$contents = get_web_page($url);
echo '<pre>' . print_r($contents,true) . '</pre>';
?>

to see what's in the array.

 

Ken

Link to comment
Share on other sites

Thanks Ken, I appreciate it. I'm still working on learning arrays and how they. I know it may have seemed like a very stupid question however consider that I though PHP was a bad ingredient in Chinese rice a month ago I think I've come along way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.