file_get_contents and %

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
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

file_get_contents and %

Post by pppswing »

Hi,

When I'm using file_get_contents with a string that contains % then I got a 403 HTTP Error.
Whereas, the same link works fine in my browser.

If I'm doing the same with a string without % then it's OK.


How can I get it works with % ? :roll:

You can check this with wikipedia files. for example:
$str = file_get_contents("http://en.wikipedia.org/wiki/Josef_Ho%C ... A9-Wronski");


Thanks
:D
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

file_get_contents()
Just read carefully 'Description' section
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

Post by pppswing »

Well, the file name is already URL encoded and I can't see what's wrong then !
:roll:
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Hmm.. Yes. Sorry. Very often it's the main reason. Have you tried to get another pages from Wiki (without special characters in the url)?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

php doesn't send a user-agent by default and wikipedia doesn't like that

Code: Select all

<?php
$default_opts = array(
	'http' => array(
		'user_agent' => 'php',
	)
);
$ctx = stream_context_create($default_opts);
readfile('http://en.wikipedia.org/wiki/Josef_Ho%C3%ABn%C3%A9-Wronski', false. $ctx);
?>
pppswing
Forum Commoner
Posts: 33
Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia

Post by pppswing »

You're right about the context, but as I'm working in php4 and context had been added later in php5,
I'm using then:

ini_set('user_agent','Mozilla: (compatible; Windows XP)');

And now it works fine!

What is curious is that context is not required if the URL doesn't have a %.

Thanks.
:P
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please consider upgrading to php5
http://www.gophp5.org/
Post Reply