file_get_contents() Doesn't Work?
Moderator: General Moderators
file_get_contents() Doesn't Work?
Hello All,
So I've just been trying to execute the following code but for whatever reason, the file_get_contents command just isn't doing its job and I have no clue why. If anyone could help that'd be great. Here's the code I'm using:
<?php
$url = 'http://www.php.net/index.php';
$content = file_get_contents($url);
?>
Also I'm using version 5.1.6 which I'm almost positive php.net said file_get_contents() exists for. If there is an alternative route to get the source code of a page, that'd be welcome as well.
-bps
So I've just been trying to execute the following code but for whatever reason, the file_get_contents command just isn't doing its job and I have no clue why. If anyone could help that'd be great. Here's the code I'm using:
<?php
$url = 'http://www.php.net/index.php';
$content = file_get_contents($url);
?>
Also I'm using version 5.1.6 which I'm almost positive php.net said file_get_contents() exists for. If there is an alternative route to get the source code of a page, that'd be welcome as well.
-bps
This is the error that I get:
Warning: file_get_contents(http://www.php.net/index.php) [function.file-get-contents]: failed to open stream: Permission denied in /var/www/html/MC/test.php on line 7
Warning: file_get_contents(http://www.php.net/index.php) [function.file-get-contents]: failed to open stream: Permission denied in /var/www/html/MC/test.php on line 7
I found someone with the EXACT same problem as me here: http://www.phpfreaks.com/forums/index.p ... 435.0.html
There was no answer, but would anyone happen to know how to fix it based off the information provided there?
There was no answer, but would anyone happen to know how to fix it based off the information provided there?
May be your webhoster does not allow connections to the outside world.
What doesprint?
What does
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$errmsg=$errno='';
$sd = fsockopen('www.php.net', 80, $errno, $errmsg);
echo "errno=$errno, errmsg=$errmsg";Here is the output from what you gave me:
Warning: fsockopen() [function.fsockopen]: unable to connect to http://www.php.net:80 (Permission denied) in /var/www/html/MC/test.php on line 6
errno=13, errmsg=Permission denied
Also, the server is my own, I'm not hosted. So if anyone knows how to fix this on a configuration level, that would be greatly appreciated as well.
Warning: fsockopen() [function.fsockopen]: unable to connect to http://www.php.net:80 (Permission denied) in /var/www/html/MC/test.php on line 6
errno=13, errmsg=Permission denied
Also, the server is my own, I'm not hosted. So if anyone knows how to fix this on a configuration level, that would be greatly appreciated as well.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Code: Select all
ob_start();
$ch = curl_init($url);
curl_exec($ch);
curl_close($ch);
$content = ob_get_contents();
ob_end_clean();And if you don't if your server has it.. Run the script. You'll know soon enough. :-p
feyd | Please use
This should just print out the contents of google's page to my browser, right? I didn't get anything, theres no errors, just nothing comes up though. Any ideas?
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
First off thanks for the help, it looks like I'm really close.
I went ahead and just did a "curl http://www.google.com" on the command line and it worked--so looks like curl is working fine. However when I tried your code:Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ob_start();
$url = 'http://www.google.com';
$ch = curl_init($url);
curl_exec($ch);
$content = ob_get_contents();
ob_end_clean();
echo $content;
?>This should just print out the contents of google's page to my browser, right? I didn't get anything, theres no errors, just nothing comes up though. Any ideas?
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Umm.... Like I said, I don't use output buffering much. Try it without the output buffering and see what you get.
Code: Select all
$url = 'http://www.google.com';
$ch = curl_init($url);
curl_exec($ch);feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm at a loss. I tried doing the code on a different server and it worked perfectly. I have no clue what could be wrong with my server.
Here's the code that worked for me on another server....now if I could only get it to work on both:Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$url = 'http://www.google.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
echo $str;
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm