Can anyone explain why the following code returns a warning:
<?php
echo file_get_contents("http://google.com");
?>
I get a Warning:
Warning: file_get_contents(http://google.com):
failed to open stream: No such file or directory on line 2
See codepad
As an alternative, you can use cURL, like:
$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
See: cURL
Try this function in place of file_get_contents():
<?php
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
It can be used just like file_get_contents(), but uses cURL.
Install cURL on Ubuntu (or other unix-like operating system with aptitude):
sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart
See also cURL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With