file_get_contents – failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

I was recently wrote Facebook application using graph API , In this case my application need to update settings in developer (www.developers.facebook.com).

 php5.4

The URL was like this

https://graph.facebook.com/131061730397xxx?page_tab_default_name=Personaliti rakan2&page_tab_url=http://domain.com/fbapps/v5/001&secure_page_tab_url=https:/domain.com/fbapps/v5/?id=001&privacy_policy_url=https://domain.com/fbapps/v5/privacy-policy.htm&website_url=http://domain.com/fbapps/v5/001&method=post&access_token=131061730397177|SF7i4WLEeqLEM2TQrc6wiTGDfET

 If you try the URL in normal browser it can run without  problems but when you run the URL using file_get_contents it would produce 404 errors.

Warning: file_get_contents(xxxx): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/mahadir/fbapps/index.php on line 35

Actually this errors came out because of the parameter in the URL is separated, if you looked carefully at

page_tab_default_name=Personaliti rakan2

you could see that there is a space between it.

So the solution is quite easy, you just need url_encode the parameter itself.

 

<?php
$page_tab_default_name = urlencode("Personaliti rakan2");
$page_tab_url = urlencode("http://domain.com/fbapps/v5/001");
$secure_page_tab_url = urlencode("https://domain.com/fbapps/v5/?id=001") ;
$privacy_policy_url = urlencode("https://domain.com/fbapps/v5/privacy-policy.htm");

$url = "https://graph.facebook.com/".$appid."?page_tab_default_name=".$page_tab_default_name."&page_tab_url=".$page_tab_url."&secure_page_tab_url=".$secure_page_tab_url."&privacy_policy_url=".$privacy_policy_url."&website_url=".$page_tab_url."&method=post&".$access_token;

echo file_get_contents($url);

?>

I hope this solution can save out your time on troubleshooting. 🙂

 

 

 

 

 

 

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

In an effort to prevent automatic filling, you should perform a task displayed below.