0

I am trying to retrieve details of a page/profile from Facebook into my PHP application. So I retrieve the id first and then run the following query. However I am getting an error.

I am getting the following error:

Warning: file_get_contents(https://graph.facebook.com/v2.8/40444963499?fields=id,name,picture.width(700).height(700),albums.limit(5){name,photos.limit(2){name, picture}},posts.limit(5)&access_token="my access token here"): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

However, if I run the same URL on the browser, I get back the JSON correctly.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33

2 Answers2

1

Windows almost always has problems with SSL certificates, I wouldn't recommend you do this for your production site, but during development it's fine. By disabling the SSL check you're effectively saying you don't care if the site has a valid SSL certificate, which means that if someone was trying to impersonate graph.facebook.com you would be communicating with this site that is likely trying to steal your access token.

$context = stream_context_create(array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    )
));

file_get_contents('https://graph.facebook.com/v2.8/40444963499?fields=id,name,picture.width(700).height(700),albums.limit(5){name,photos.limit(2){name, picture}},posts.limit(5)&access_token=FB_ACCESS_TOKEN', null, $context);

Now if you're interested in actually fixing the problem on your machine, then review this answer: PHP - SSL certificate error: unable to get local issuer certificate

Community
  • 1
  • 1
Jonathan
  • 2,778
  • 13
  • 23
  • I don’t think this is an SSL issue. If it was, there should be no 400 response code. If client and server can not correctly set up the secure connection, then they won’t be talking HTTP at all. – CBroe Mar 01 '17 at 08:30
  • I would suggest to pass in an HTTP context with `ignore_errors` set to true, so that the request body is fetched by file_get_contents as well - pretty sure there will be further explanation of the error in there. – CBroe Mar 01 '17 at 08:33
  • Yeah, the thing is, all other requests are working fine. – Anindya Dutta Mar 02 '17 at 19:54
0

So the error was that there was a space in between two of the parameters passed in the url. It wasn't showing on the web browser because I assume it does not print the space but because of the space, the file_get_contents wasn't working.

Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33