Parsing JSON in PHP is not giving response - php

So I am trying to get some information from a JSON file. But for some reason there is no actual response with the information that I need.
This is the code im running.
$json_object = file_get_contents('http://steamcommunity.com/profiles/'.$steamID.'/inventory/json/730/2');
$json_decoded = json_decode($json_object);
echo $json_decoded->success;
I get the steamID from a cookie. But if you want to see how the JSON looks, then you can check out this link:
http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2

The problem is that the URL you are checking is redirected to another URL, file_get_contents() doesn't support following redirects AFAIK, so you should better use cURL
Check this answer https://stackoverflow.com/a/4324018/5658508

the problem is like the others tell you is in this link http://steamcommunity.com/profiles/76561198031313244/inventory/json/730/2
i just monitor the response, so when you visit the link you get a 302 Moved Temporarily http response to the new link which is http://steamcommunity.com/id/SANDISS/inventory/json/730/2
you can try to get contents using file_get_contents if you have the link not just a steam id, or use curl library

Related

PHP get Response from a requested URL

I am implementing a OneClick Login function. From a existing function I am getting a URL, So to get the token I should send it, when I copy paste to the browser I am getting the token. I am using php so what is the way to get the token from PHP code.
You can use file_get_contents like this:
<?php
$response = file_get_contents("https://your-url.com/");

json_decode with file_get_contents

I am trying to use PHP to echo the contents as plain text so that I can use it my application.
I am trying to obtain the contents of http://www.revctrl.com/api/projects/231 which is in jSON format then convert it to an associated array then manually echo the contents in a nice and neat format. But for some reason, file_get_contents is returning NULL everytime.
I have no clue what is wrong with the code.
$jsonData = json_decode(file_get_contents("https://www.revctrl.com/api/projects/231"), true);
The link works in the browser. The jSON output is valid (checked using http://jsonlint.com/).
Any idea why I get a null from file_get_contents?
Is there any server setting that needs to be set to allow outside links to be accessible?
file_get_contents just discards the server response body in case the HTTP status code indicates the some kind of error; and standard PHP error reporting won’t give you a much of a clue either in case you’re using the function to make an HTTP request.
You can pass in an HTTP context via stream_context_create, setting the option ignore_errors to true – then you will get the error message description the server has likely send in the response body returned.
Use var_dump to output it – then you should be able to figure out what goes wrong on the remote end.

Execute external Get url request

So, I have this method where I need to call an external url (different domain). It's something like http://192.168.2.2:9090/send?num=100&txt=text; Is there any way to do this without using curl?
I guess I should clarify that I have tried using curl with the yii-curl extension but somehow it doesn;t seem to work. It works when I supply a whole formatted url, but if I try to modify the url with params for num and txt, it doesn't work for some reason. Preferably I am looking for a solution without curl, but if that is not possible I could use some help with how to format and execute a proper url so I can also supply params to the url. Thanks.
Edit: I don't think file_get_contents() will work as the url is actually to an SMS gateway that sends sms. the phone number and sms text is supplied as params. Let me know if I am guessing it wrong.
Edit 2: This is what I tried after the suggestions here.
public function sendTXTSMS($sentToNum,$text)
{
$construct_url="http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";
file_get_contents($construct_url);
}
And then calling it like,
$text='Lorem ipsum dolor ........ ';
$this->sendTXTSMS(XXXXXXXXXX,$text)
XXXXXXXXXX is of course the phone number masked here.
Now I am getting an HTTP request failed! HTTP/1.1 400 Bad Request error. allow_url_fopen is enabled and I can access the url fine by typing it on a browser. Also tried using urlencode on the url.
If it's a GET request you can use file_get_contents($url);.
If you need more options you can try the HTTP library, but there's little reason to not directly use libcurl. It's standard practice.
The fact it's connecting to a service related to SMS is irrelevant. If it's a URL for a web service on a server you can connect to, you can make a request to it.
file_get_contents() will work if allow_url_fopen is enabled in php.ini, but I think your problem is this:
It works when I supply a whole formatted url, but if I try to modify
the url with params for num and txt, it doesn't work for some reason.
You need to encode the data:
$test = urlencode($text);
$sentToNum = urlencode($sentToNum);
$construct_url = "http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";
Yes you can use file_get_contents http://php.net/manual/en/function.file-get-contents.php
Using curl should be a better option since you can easily deal with http status code...
But with or without curl, you need to build your url correctly, urlencode should be used on params, not on url :
$sentToNum = urlencode($sentToNum);
$text = urlencode($text);
$construct_url="http://192.168.2.2:9090/send?num={$sentToNum}&txt={$text}";

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?

Need help understanding Google Maps API URL?

i am kind of new to API thing, and i need help understanding on what exactly is happening with the below Code.
$address = 'Bhatkal, Karnataka, India';
$requestUrl = 'http://maps.google.com/maps/geo?output=xml&key=aabbcc&oe=utf-8&q='.urlencode($address);
$xml = simplexml_load_file($requestUrl);
i understand that HTTP is capable of sending Request and getting response in return isn't it? what i am unable to understand is the third and last function that is $xml = simplexml_load_file($requestUrl); when i do a print_r($xml) i get an object in return which prints all the object details i got back as response,
how does the function process the
URL?
does it use CURL (i have very less idea about what is CURL).
and where do i look up for Google Maps API URL?
That function does not process the request (nor the URL), only the response, Google processes the URL that, the function just "visit's" it. You can do as well: here. The XML file you see here is ending up in the variable $xml, parsed.
EDIT: the URL in this post is not working too well, because of the key parameter
simplexml_load_file internally uses the fopen wrapper and opens the remote xml that would be produced by the url and then converts into an array for php to easily use.
The response object will help you to extract the data from the response.
Check out the details of Google Maps API

Categories