fopen on a file/url with blanks/whitespaces

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
alfmarius
Forum Newbie
Posts: 16
Joined: Thu Jul 10, 2008 5:14 am

fopen on a file/url with blanks/whitespaces

Post by alfmarius »

Hi :)

I just stumbled upon this:

search.php:

Code: Select all

<?php
print $_GET['arg'];
?>
test.php:

Code: Select all

<?php
print "a) " . file_get_contents("http://server/search.php?arg=oneword", "r");
print "b) " . file_get_contents("http://server/search.php?arg=two words", "r");
?>
test.php outputs:

Code: Select all

a) oneword
b) Warning: file_get_contents(http://server/search.php?arg=two words) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in ...
I can live with the fact that _this_is_how_it_is_ and do some query replace on whitespaces, but before I implement that scheme I just wanted to check if anyone know if there is a way to make this work directly? (I have seen %20 used alot for whitespaces, without really understanding why, is this why?)
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: fopen on a file/url with blanks/whitespaces

Post by lonelywolf »

my output from your code:
a) onewordb) two

--------------------
in php manual, space is not valid in parameter.
http://php.net/file_get_contents
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Re: fopen on a file/url with blanks/whitespaces

Post by dhrosti »

I have seen %20 used alot for whitespaces, without really understanding why, is this why?
this is encoded whitespace, you find similar characters with colons and ampersands, etc.

two functions in php can be used to encode/decode the urls, strange as it might sounds, they are called urlencode() and urldecode() respectively.
alfmarius
Forum Newbie
Posts: 16
Joined: Thu Jul 10, 2008 5:14 am

Re: fopen on a file/url with blanks/whitespaces

Post by alfmarius »

Thanks lonelywolf and dhrosti for your interest!
lonelywolf wrote:my output from your code:
a) onewordb) two
Hm, strange.. What version of php are you using? I'm using 5.2.0-8

I have come to the conclusion that whitespace in urls are generally not tolerated, and will lead to unexpected results. Why it is so difficult to make it work with urls i cannot tell.

I noticed that when using the plus sign "+" in the url, php will replace it with whitespace, so i guess this is the solution.

But I couldn't find this behavior documented anywhere in the php docs.. Does someone know if this is documented so I can treat it as a fact?

Code: Select all

http://server/file.php?arg=one+two+three
$_GET = "one two three"
Post Reply