Page 1 of 1
file_get_contents() Doesn't Work?
Posted: Tue May 29, 2007 10:38 pm
by bps67925
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
Posted: Tue May 29, 2007 10:46 pm
by feyd
Please define "doesn't work."
Posted: Tue May 29, 2007 10:53 pm
by bps67925
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
Posted: Wed May 30, 2007 12:54 am
by bps67925
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?
Posted: Wed May 30, 2007 1:23 am
by volka
May be your webhoster does not allow connections to the outside world.
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";
print?
Posted: Wed May 30, 2007 2:57 pm
by bps67925
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.
Posted: Wed May 30, 2007 3:01 pm
by superdezign
cURL?
(I'm messing around with its functions and options right now... Amazing that I've never used it before!

Try it.)
Posted: Wed May 30, 2007 3:18 pm
by bps67925
Could you give me an example of how to do the same thing as file_get_contents with the curl methods--I have no idea how to use it at all....and don't even know if my server supports it yet.
Posted: Wed May 30, 2007 3:30 pm
by superdezign
Code: Select all
ob_start();
$ch = curl_init($url);
curl_exec($ch);
curl_close($ch);
$content = ob_get_contents();
ob_end_clean();
I don't use the output buffer much, so you may not need the ob_end_clean().
And if you don't if your server has it.. Run the script. You'll know soon enough. :-p
Posted: Wed May 30, 2007 3:40 pm
by bps67925
feyd | Please use 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
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]
Posted: Wed May 30, 2007 3:44 pm
by superdezign
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);
Posted: Wed May 30, 2007 3:55 pm
by bps67925
feyd | Please use 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
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]
Posted: Wed May 30, 2007 4:07 pm
by superdezign
Maybe they don't have the SSL DLLs installed on your server... I'm clueless as to how to know for sure.
Do they have a help section?
Posted: Thu May 31, 2007 12:21 am
by volka
fsockopen tries to create a socket connection. curl must do the same thing. If socket connections are not permitted neither method can work.
Please ask your provider.