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
pppswing
Forum Commoner
Posts: 33 Joined: Thu Jun 10, 2004 2:04 am
Location: Tallinn, Estonia
Post
by pppswing » Fri Jul 06, 2007 7:46 am
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 % ?
You can check this with wikipedia files. for example:
$str = file_get_contents("
http://en.wikipedia.org/wiki/Josef_Ho%C ... A9-Wronski ");
Thanks
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Fri Jul 06, 2007 7:49 am
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 » Fri Jul 06, 2007 7:54 am
Well, the file name is already URL encoded and I can't see what's wrong then !
Gente
Forum Contributor
Posts: 252 Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:
Post
by Gente » Fri Jul 06, 2007 8:08 am
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)?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Jul 06, 2007 8:12 am
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 » Fri Jul 06, 2007 8:34 am
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.