fOpen returns 400

Hello,

Can you tell me why the following code returns an error?

<? $handle = fopen('http://www.canada.com/sports/Crosby back race/5745823/story.html', "r"); //Error returned //fopen(http://www.canada.com/sports/Crosby back race/5745823/story.html) //[function.fopen]: failed to open stream: HTTP request failed! //HTTP/1.0 400 Bad Request in /home/content/87/8235887/html/rtnpage.php ?>

Also, how do you fix this type of error?

Thanks,

Neil

You need to encode spaces in URL - what you have now is not valid URI. Browsers substitute spaces automatically, but if you want to make request from your php code, you need to do this manually. I’d suggest to use urlencode(), but not in your case - this would encode all the slashes etc. All you need is to replace all the spaces with %20.

Here is how your code will look:
[php]<?php
$handle = fopen(‘http://www.canada.com/sports/Crosby%20back%20race/5745823/story.html’, “r”);
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service