Failing API query outputs API password to the user frontend - php

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

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.

file_get_contents failing when was working earlier

I am trying to access multiple json files provided by steam for the market price of an item for CSGO. I am using a first file_get_contents which works:
$inventory = file_get_contents('http://steamcommunity.com/profiles/' . $steamprofile['steamid'] . '/inventory/json/730/2');
but the 2nd onwards doesn't work:
$marketString = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' . urlencode($json_a->{'rgDescriptions'}->$rgDescrId->{'market_hash_name'}));
However I get the error on all items for example:
Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Negev%20|%20Nuclear%20Waste%20(Minimal%20Wear)): failed to open stream: HTTP request failed! HTTP/1.0 429 Unknown in /home4/matt500b/public_html/themooliecommunity.com/CSGO/index.php on line 24
I can confirm that allow_url_fopen is on
Pasting the following url into a browser shows that the url works
http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Negev%20|%20Nuclear%20Waste%20(Minimal%20Wear)
Please note that about 1 hour ago this worked but now throwing an error, any suggestions?
You've got response with status 429 Too many requests
The user has sent too many requests in a given amount of time ("rate
limiting").
So this site can just block too frequent reference to his API
A HTTP 429 is a too many request warning, it's not an error, just a note to tell you you've over done it a little. You'll have to either wait a while or if it's your own server then adjust it's settings to allow for more requests.

How to do a composite search in bing search API?

I'm trying to do a composite search in bing search API using php. From this documentation i got this.
https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=%27web%2bnews%27&Query=%27XBox%27&$top=1 This gives some result which i don't know to parse and show the result. I tried `simplexml_load_file()` to parse but no use.
If i mention json format request like below i get a bad request sent error( failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request) and file_get_contents() error
https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=%27web%2bnews%27&?$format=json&Query=%27XBox%27&$top=1
How to get the result with json?
Correct query for you is: (I removed "?" before $format=json)
https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=%27web%2bnews%27&$format=json&Query=%27XBox%27&$top=1

Facebook API call works in browser, but not in code

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

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