Page 1 of 1

file_get_contents and %

Posted: Fri Jul 06, 2007 7:46 am
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

Posted: Fri Jul 06, 2007 7:49 am
by Gente
file_get_contents()
Just read carefully 'Description' section

Posted: Fri Jul 06, 2007 7:54 am
by pppswing
Well, the file name is already URL encoded and I can't see what's wrong then !
:roll:

Posted: Fri Jul 06, 2007 8:08 am
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)?

Posted: Fri Jul 06, 2007 8:12 am
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);
?>

Posted: Fri Jul 06, 2007 8:34 am
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

Posted: Fri Jul 06, 2007 8:41 am
by volka
Please consider upgrading to php5
http://www.gophp5.org/