Fellow Programmers,
I did test both code blocks separately and they did redirect me to apple.com.
Sample 1:
//gets the data from a URL
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_url('http://www.apple.com/');
Sample 2:
// Make a HTTP GET request and print it (requires allow_url_fopen to be enabled)
echo file_get_contents('http://www.apple.com/');
I was just curious to learn why anyone would bother using the long version if the short version can do the same job.
I got my answer from someone that the short version is risky. This is what I learnt:
file_get_contents() using a URL is not guaranteed to work in all situations, as it depends on a configuration setting to allow it to use HTTP (which is sometimes disabled for security reasons). cURL, on the other hand, should normally work, as long as the PHP cURL extension is installed.
I’ve also learnt now that, writing the following for each and every url to fetch can be tedious:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
And so, best to url_setopt_array that takes an array of options and sets them all at once. Like one programmer’s contributed code sample below:
<?php
function get_url($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// get_url2 is the same as get_url except curl options are contained in an
// array and set using curl_setopt_array
function get_url2($url)
{
$ch = curl_init();
if($ch === false)
{
die('Failed to create curl object');
}
$curlOptions = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 5);
curl_setopt_array($ch, $curlOptions);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// Call get_url or get_url2, but not both. Comment and uncomment as needed to experiment.
// Change $myUrl variable for different websites, see what comes back as a blank page. Note that
// there is no guarantee that you'll always get the same blank and non-blank pages as I do or
// the same results every time. Lots of factors.
$myUrl = 'http://www.google.com'; // non-blank, but incomplete page (no Google logo)
//$myUrl = 'http://www.daniweb.com'; // blank page
//$myUrl = 'http://www.apple.com'; // blank page
// echo get_url($myUrl);
echo get_url2($myUrl);
?>
Thanks for everyone’s inputs in this forum and others. 
I hope I remember them all!!! Don’t blame me if I don’t! Lol!