file_get_contents() Doesn't Work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

file_get_contents() Doesn't Work?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Please define "doesn't work."
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post 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
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

cURL?

(I'm messing around with its functions and options right now... Amazing that I've never used it before! :-D Try it.)
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post by bps67925 »

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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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);
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post by bps67925 »

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]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply