Facebook API call works in browser, but not in code - php

I am working on a Facebook application and I have a script that sends notifications to users whenever an important event has occurred. This script goes through a list of users and fires off notifications in a loop. Now, when I activate the script, nothing happens, and I get the following error in my log concerning the url's that I tried to access:
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request\r\n
However, when I copy and paste a supposedly bad url into the browser, I get a message of success and the notification appears on Facebook. My question here is, is there any reason the url would not be accepted from a pHP program, but only from browser access?
Here is a code snippet:
$apprequest_url = "https://graph.facebook.com/".
$user_id.
"/notifications?href=apps.facebook.com/vmoodletest".
"&template='sigh'&".
$app_access_token."&method=post";
$result = file_get_contents($apprequest_url);
Here is the resulting url:
https://graph.facebook.com/xxxxxxxxxxxx/notifications?href=apps.facebook.com/vmoodletest&template='blah blah posted a comment on Does it work? in the -General Forum- forum in Senior Project.'&access_token=xxxxxxxxxxxx|xxxxxxxxxxxxxxx&method=post

Related

Unable to get json from instagram: PHP failed to open stream: HTTP request failed

I'm trying to develop and Instagram application but I'm struggling to get a json inserting the access token via variable.
That's my code:
$personal = json_decode(file_get_contents('https://api.instagram.com/v1/users/self/?access_token={$accesstoken}'));
And the error that I receive is this:
PHP Warning: file_get_contents(https://api.instagram.com/v1/users/self/?access_token={$accesstoken}): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
I already tried to see if I get the variable $accesstoken from the previous form and it's all right, because if I echo-it on the page it pops up.
I tried the function preg_replace to understand if the problem were the white spaces, but nothing.
I don't want to use cURL if it is not mandatory.
What's wrong with the code (and me)?
Thanks in advance!
Edit:
As I answered to #FirstOne: yes, I already tried to put the variable (access token) manually and in this way it works.

Google drive api file_get_contents and refferer

I am trying to list files from google drive folder.
If I use jquery I can successfully get my results:
var url = "https://www.googleapis.com/drive/v3/files?q='" + FOLDER_ID + "'+in+parents&key=" + API_KEY;
$.ajax({
url: url,
dataType: "jsonp"
}).done(function(response) {
//I get my results successfully
});
However I would like to get this results with php, but when I run this:
$url = 'https://www.googleapis.com/drive/v3/files?q='.$FOLDER_ID.'+in+parents&key='.$API_KEY;
$content = file_get_contents($url);
$response = json_decode($content, true);
echo json_encode($response);
exit;
I get an error:
file_get_contents(...): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
If I run this in browser:
https://www.googleapis.com/drive/v3/files?q={FOLDER_ID}+in+parents&key={API_KEY}
I get:
The request did not specify any referer. Please ensure that the client is sending referer or use the API Console to remove the referer restrictions.
I have set up referrers for my website and localhost in google developers console.
Can someone explain me what is the difference between jquery and php call and why does php call fails?
It's either the headers or the cookies.
When you conduct the request using jQuery, the user agent, IP and extra headers of the user are sent to Google, as well as the user's cookies (which allow the user to stay logged in). When you do it using PHP this data is missing because you, the server, becomes the one who sends the data, not the user, nor the user's browser.
It might be that Google blocks requests with invalid user-agents as a first line of defense, or that you need to be logged in.
Try conducting the same jQuery AJAX request while you're logged out. If it didn't work, you know your problem.
Otherwise, you need to alter the headers. Take a look at this: PHP file_get_contents() and setting request headers. Of course, you'll need to do some trial-and-error to figure out which missing header allows the request to go through.
Regarding the referrer, jQuery works because the referrer header is set as the page you're currently on. When you go to the page directly there's no referrer header. PHP requests made using file_get_contents have no referrer because it doesn't make much sense for them to have any.

Ajax request fails from FB canvas but works on domain (CodeIgniter)

I have the following problem. I have a working PHP/CodeIgniter app and I'm integrating it into Facebook now.
However, an AJAX POST request fails with error 400, and I see in the console that CodeIgniter throws an error saying The URI you submitted has disallowed characters..
This is the URL: https://domain-name.com/apps/main/go_to_game/id=453494284796333&redirect
There are no special characters or anything.
What makes it more confusing:
1) the request works when I open the app on the domain directly (and not through FB)
2) a request to a similar URL works even when sent from FB. This is the URL: https://domain-name.com/apps/main/get_user_stuff/
This is the code that sends the request, pretty basic stuff:
$.post(base+"main/go_to_game/"+gid,function(resp){
//handle response
});
Where base is https://domain-name.com/apps/ and gid is an integer. Any ideas?

Failing API query outputs API password to the user frontend

I have a website with a user input. After the user inputs something, an API (JSON) is queried based on the input.
Every now and then the API query fails and outputs the query URL.
file_get_contents(http://...id=...password=...&input=...): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /www/htdocs/....php
The query URL contains a password which is, of course, secret. How can I prevent the page to output the failed request?
Best
Benjamin
You should have error reporting off for a production server, but as a quick fix you can use the error suppression symbol #:
$resp = #file_get_contents(...);
Obviously you will still need to handle a failed call by checking the return value:
if($resp==false)//failed

Sending a call to facebook graph api does not return anything when an error occurs

I am using graph api to get a list of albums of a user. Now in case the user signs out or has changes password or any error condition, I do not get any response whereas some error code should be returned. The graph API url when opened in the browser shows the error but on using file_get_contents() i get error 400. On using curl, I get an empty response. Is there any way that I can find out when an error occurs and the reason for it?
The graph API url when opened in the browser shows the error but on using file_get_contents() i get error 400.
Well, that is the HTTP status code that Facebook sends with Oauth errors – nothing wrong about that.
But file_get_contents by default does not show you the actual HTTP response body content in case of an HTTP status codes that signals an error …
… which is just another reason, why one should use the official PHP SDK, which handles all of that nicely and presents you with an exception that tells you exactly what is up.
So do it, please.
(Even with file_get_contents one can get the body content of the response, when setting a “stream context” with the according option first. But using the PHP SDK is way simpler, so again: Please do it.)
I used curl instead of file_get_contents and the issue got resolved.
file_get_contents() gave me error many times when I put https instead of http or If I forgot to put any of them.
Your URL from which you get the content must have "http" at the starting, like:
file_get_contents("http://www.mysite.com");
If this solves it?

Categories