How to extract JSON from URL? - javascript

I'm looking to extract JSON data from a server URL that looks something like this:
https://dev.randomdomain.com/subdomain
The URL itself outputs the data like such:
[
{ "name": "Not configured",
"mac_address": "####c##c####",
"online": "false",
"rate": "Not configured"},
{ "name": "Not configured",
"mac_address": "####c##c####",
"online": "false",
"rate": "Not configured"},
]
The outputed data changes and will need to be updated accordingly. I'm wondering what is the best method to retrieve the JSON data from the URL and keep it updated? I've looked at the jQuery getJSON function, and I'm not sure that will work as the URL is simply outputting JSON, it's not a .json file.
Can what I described be done using JavaScript, jQuery or any other open methodology?
Thanks.

$,getJSON('url',function(data){
alert(data);
// or alert("sample text"); would do if the json file is handled well enough.
// if no alert is shown, it's either you're using chrome with an offline version of
// your project or you're having an invalid json file
});
May I ask what browser are you using? $.getJSON won't work in Chrome if you are doing it in offline mode.
I also got frustrated why the json file not being processed even if I don't use Chrome - e.g. i use FF or IE.
I discovered that you shouldn't put comments inside the json file as it makes it invalid. I think, it's because .getJSON really parses data inside the JSON file as text, not as a javascript file, so it will also run through the comment (if you have any), and for sure won't understand it, so I suggest you remove comment block inside your json files.

Have you even tried using $.getJSON()? It does exactly what you need it to do, it simply reads the response from the server and returns a json object.

It is not very clear from your question if you want to process the data on client side (browser etc.) or on server side for offline processing.
For accessing and processing this data on server side, you can use curl/libcurl to pull data and decode it to an object in language of your choice. For example in php:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://dev.randomdomain.com/subdomain");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
data_as_php_object = json_decode($output);
?>

Firstly, you're actually trying to parse JSON from a response. The url is the thing you request a response from. Here is a simple example using getJSON:
$.getJSON('https://dev.randomdomain.com/subdomain', function(data) {
var output = "";
$.each(data, function(index, node) {
output += node.name + ": " + node.mac_address + "<br/>";
});
document.write(output);
});
http://jsfiddle.net/YwPzW/1/

The function $.getJSON() is to put the string in JSON format into a javascript object. So if you want to get the data, you can try as:
$.getJSON("https://dev.randomdomain.com/subdomain",function(data){
alert(data[0].name);
});

Do you trust the source of the JSON string. If you do, just use
alert(eval(dropStringHere));
The getJSON jQuery method should also work fine.

Related

PHP $_GET and underlines

I have a very short piece of PHP that I use to make HTTP requests from JavaScript.
<?php echo file_get_contents($_GET['url']); ?>
I have used it successfully in a few projects, but am running into a problem with making requests in my current project. Based on my searching, I believe it may be caused by the underscore in the request, though through my searching and not knowing PHP, I have not been able to confirm that.
Below is an example of what I am doing from JavaScript:
$.get("grabber.php?url=" + "http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW", function(forecast) {
console.log(forecast);
});
If I copy the url and put in it in a browser, I get back the JSON that I requested. When I use the code above, I end up getting an error message from NOAA:
Wrong Product : Product cannot be null or empty Wrong Time zone: Time zone cannot be null or empty Wrong Unit:Unit cannot be null or empty Wrong Format: Format cannot be null or empty Wrong Date: The beginDate cannot be null or empty
Do I need to use a regex for the underscore in PHP? Is there some other issue that I do not understand?
Thanks.
You need to send it encoded, which will convert all the underscores/spaces/ampersands etc. with their encoded equivalents:
var url = "http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW";
$.get("grabber.php?url=" + encodeURIComponent(url), function(forecast){
console.log(forecast);
}
Using encodeURIComponent() on that URL shows:
http%3A%2F%2Ftidesandcurrents.noaa.gov%2Fapi%2Fdatagetter%3Fstation%3D8573364%26begin_date%3D20160202%26end_date%3D20160203%26product%3Dpredictions%26units%3Denglish%26time_zone%3Dgmt%26format%3Djson%26application%3Dposeidonweathercom%2B%26datum%3DMLLW
Alternatively, if you just want to access the JSON data and handle it within the JavaScript function, you can retrieve the data via the URL directly, without having to encode the URL:
$.get("http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW", function(forecast) {
console.log(forecast);
});
Um why do you even need your php code ... the code below will work just fine and eliminate your server overhead.
$.get("http://tidesandcurrents.noaa.gov/api/datagetter?station=8573364&begin_date=20160202&end_date=20160203&product=predictions&units=english&time_zone=gmt&format=json&application=poseidonweathercom+&datum=MLLW", function(forecast) {
console.log(forecast);
});

file_get_contents('php://input'); with application/x-www-form-urlencoded;

I've read a few questions about the subject here on but couldn't find the answer I'm looking for.
I'm doing some $.post with jQuery to a PHP5.6 server.
$.post('/', {a:100, b:'test'}, function(data){
}, 'json');
The encoding from the console is
Content-Type application/x-www-form-urlencoded; charset=UTF-8
If I try to read the POST data with a regular $_POST, PHP5.6 alerts me
PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead
So then I've tried the suggestion, added always_populate_raw_post_data = -1 in php.ini and
json_decode(file_get_contents("php://input"));
PHP5.6 alerts me that it is invalid
PHP Warning: First parameter must either be an object or the name of an existing class
So I've dumped file_get_contents("php://input") and it's a string.
a=100&b="test"
So I've parsed the string and encoded then decoded
parse_str(file_get_contents("php://input"), $data);
$data = json_decode(json_encode($data));
var_dump($data);
And THEN I finally have my $data as an object and not an array, as a true JSON object.
I've resorted to keep on using $_POST for now... But then I'm wondering about upgrading PHP..
The question here is that, is there a straighter forward solution to this or does it mean using file_get_contents("php://input") also means doing the parsing decoding encoding shenanigans?
Edit: so it appears this doesn't work either on multi levels json's.
Consider the following:
{"a":100, "b":{"c":"test"}}
As sent in Ajax/Post
{a:100, b:{c:"test"}}
Doing
parse_str(file_get_contents("php://input"), $post);
var_dump($post);
Will output
array(2) {
["a"]=>string(8) "100"
["b"]=>string(16) "{"c":"test"}"
}
Or doing (as suggested)
parse_str(file_get_contents("php://input"), $post);
$post= (object)$post;
Will output
object(stdClass)#11 (2) {
["a"]=>string(8) "100"
["b"]=>string(16) "{"c":"test"}"
}
How do I transform file_get_contents("php://input") into a true object with the same "architecture" without using a recursive function?
Edit2 : My mistake, the suggested worked, I got side tracked in the comments with JSON.stringify which caused the error.
Bottom line: it works with either json_decode(json_encode($post)) or $post=(object)$post;
To recap, using jQuery $.post :
$.post('/', {a:100, b:{c:'test'}}, function(data){
}, 'json');
parse_str(file_get_contents("php://input"), $data);
$data = json_decode(json_encode($data));
or
parse_str(file_get_contents("php://input"), $data);
$data= (object)$data;
No need to use JSON.stringify
Receiving serialized/urlencoded POST data in the request's POST body as you are, you've correctly transformed it into an array with parse_str() already.
However, the step of encoding then decoding JSON in order to transform that into the object (as opposed to array) you're looking for is unnecessary. Instead, PHP will happily cast an associative array into an object of class stdClass:
parse_str(file_get_contents("php://input"), $data);
// Cast it to an object
$data = (object)$data;
var_dump($data);
A little more information is available in the PHP documentation on type casting.
In order to send raw json data, you have to stop jQuery from url-encoding it:
data = {"a":"test", "b":{"c":123}};
$.ajax({
type: 'POST',
url: '...',
data: JSON.stringify(data), // I encode it myself
processData: false // please, jQuery, don't bother
});
On the php side, just read php://input and json_decode it:
$req = file_get_contents("php://input");
$req = json_decode($req);
My mistake, the suggested worked, I got side tracked in the comments with JSON.stringify which caused the error. Bottom line: it works with either json_decode(json_encode($post)) or $post=(object)$post;
The answer Michael gave is correct but side tracked me and I left the error in my code. JSON.stringify is only useful when posting the data from a form as I replied in the comment.

How to scrape a JavaScript value in PHP

I’m new to PHP and coding in general and I can’t figure this out. I’m trying to get the number of kills from this profile page.
At the moment, the string I am trying to get is:
29362
When I view the page source, this number is nowhere to be seen.
When I use inspect element, however, I find:
<td class="num">29362</td>
How can I get the content shown in inspect element instead of the content shown by viewing the page source?
In using a tool like Firebug for Firefox, or the inspector for Safari and Chrome, you can see that at page load a series of AJAX requests are made for data. Though I didn't dig through all of the data returned by those requests, I do see the data you're looking for in at least one of them:
http://uberstrike.com/profile/all_stats/631163
So at page load JavaScript makes a series of AJAX requests back to the server to get all the data, then it manipulates the DOM to insert it all into the view.
If you wanted, your PHP could directly request the URL I pasted above and json_decode the response. This would produce a data structure for you to use which includes that number in the kills_all_time property.
Quick and dirty example:
<?php
$data_url = 'http://uberstrike.com/profile/all_stats/631163';
$serialized_data = file_get_contents($data_url);
$data = json_decode($serialized_data, true);
var_dump($data['kills_all_time']);
I looked and it looks like there is no API currently, so your best method will be to do an inter-web-server http request. Get the page you want and then it is a lot of string math from there.
I would recommend using string search to find <td class="name">Kills</td> and the kills row will appear right after it. From there its simply extracting the number using string math.
To add to what JAAulde has explained, it seems like there is a method to these AJAX requests. And they are all based on the profile ID that can be found at the end of the URL:
http://uberstrike.com/public_profile/631163
Then in the Safari debugger (which is what I am using) you can see these XHR (XMLHttpRequest) requests which are directly connected to API calls:
Then looking at the data in them shows some really nicely formatted JSON. Great! No scraping! So just go through these URLs to see what you can see:
http://uberstrike.com/profile/items
http://uberstrike.com/profile/user_info/631163
http://uberstrike.com/profile/user_loadout/631163
http://uberstrike.com/profile/all_stats/631163
And looking at the all_stats endpoint shows:
"kills_all_time":29362,
Nice!
So now let’s use some PHP json_decodeing like this:
// Set the URL to the data.
$url = 'http://uberstrike.com/profile/all_stats/631163';
// Get the contenst of the URL via file_get_contents.
$all_stats_json = file_get_contents($url);
// Decode the JSON string with the 'true' optionso we get any array.
$all_stats_json_decoded = json_decode($all_stats_json, true);
// Dump the results for testing.
echo '<pre>';
print_r($all_stats_json_decoded);
echo '</pre>';
Which will dump an array like this:
Array
(
[headshots_record] => 24
[nutshots_record] => 33
[damage_dealt_record] => 6710
[damage_received_record] => 31073
[kills_record] => 50
[smackdowns_record] => 45
[headshots_all_time] => 4299
[nutshots_all_time] => 1925
[kills_all_time] => 29362
[deaths_all_time] => 16491
…
Now to get kills_all_time just do this:
// Return the 'kills_all_time'.
echo $all_stats_json_decoded['kills_all_time'];
Which gives us:
29362

get the geolocation from IP , with "ipinfodb"

i know good javascript but totally novice in json and need to do the following:
get the geolocation from IP , with "ipinfodb"
like this response:
OK;;174.88.229.95;CA;CANADA;QUEBEC;MONTREAL;H1A 0A1;45.5088;-73.5878;-04:00
i found many codes for that but all seem to be complicate and long with more options like saving the results in cookies
i want the least necessary code to retrieve those informations
for saving them in cookies and more i want to care for it my self after.. (i don't like to put code i dont understand)
the best would be a simple function that returns this information as string or array, like this
function getLocFromIP(IP){
(js + json code)
return result;
}
much thank in advance
thank you all for responding i was trying to filter out the solution, and it works but i'm not yet well satisfied of the result..
i gonna reformulate my question here:
is there i can retrive the geo location from ip with simplest way (javascript) that means without json or jquery..
ajax would be perfect but it doesnt work inter domain (different domains)
thanks again
OK;;174.88.230.88;CA;CANADA;QUEBEC;MONTREAL;H1A 0A1;45.5088;-73.5878;-04:00
The example you posted isn't valid JSON (you can check if something is valid JSON somewhere like here). That said, looking at the ipinfodb website they appear to have a way to get JSON output using their API (see here).
Once you have valid JSON, you can use JSON.parse. See here for some examples on using it.
So the general idea is
function getLocFromIP(IP){
//(js code to use ipinfodb API to get valid JSON reponse)
//(js code using JSON.parse to parse the received JSON reponse)
//(js code to get whatever you want as the result)
return result;
}
As a side note, if you use jQuery you can also use jQuery.parseJSON to parse JSON (see here) which will be more reliable if you anticipate clients using older browsers that might not support JSON.parse.
IP location XML API requires some query strings like &format=json&callback=? additional to ip address in order to get json.
Then you get json like this:
{
"statusCode": "OK",
"statusMessage": "",
"ipAddress": "175.108.204.0",
"countryCode": "JP",
"countryName": "JAPAN",
"regionName": "TOKYO",
"cityName": "TOKYO",
"zipCode": "214-002",
"latitude": "35.6149",
"longitude": "139.581",
"timeZone": "+09:00"
}
Here is a sample code (thanks to #iveoak for good template):
function getLocFromIP(IP){
//(js code to use ipinfodb API to get valid JSON response)
var url = 'http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=' + ip + '&format=json&callback=?';
var response = $.getJSON(url); // using jQuery
//(js code using JSON.parse to parse the received JSON response)
response.then(function (location) {
var info = '<ul>';
for (var key in location) {
info += '<li>' + key + ': ' + location[key] + '</li>';
}
info += '</ul>';
});
//(js code to get whatever you want as the result)
return result;
}
If you do not use jQuery, see How can I open a JSON file in JavaScript without jQuery?.
Sample (using jQuery): http://jsfiddle.net/tokkonoPapa/tevB4/
I post another answer.
This is a pure javascript solution using jsonp. Basically, it's a same concept using <script src="http://api.ipinfodb.com/..."></script>.
So you can accsess from anywhere, and do not need to handle json directly. You can simply get the javascript object.
Here is my demo: http://jsfiddle.net/tokkonoPapa/vCTpK/
UPDATE:
I/F is slightly different what you want. It uses a callback function.
getLocFromIP(ip, function(result) {
// use result
});
and the demo returns:
OK;;175.108.204.0;JP;JAPAN;TOKYO;TOKYO;214-002;35.6149;139.581;+09:00
I hope this basic solution satisfies you.

AJAX responseXML

i have a problem regarding the responseXML of ajax..
I have this code from my callback function:
var lineString = responseXML.getElementsByTagName('linestring')[0].firstChild.nodeValue;
However, the linestring can only hold up to 4096 characters max.. the remaining characters are rejected.
I dont know what to use to get all the values that the lineString
returns. its quite a big data thats why I thought of using the responseXml
of AJAX, BUT turned out it still cannot accomodate everything.
My linestring consists of lines from a logfile which I concatenated and just
put line separator. I need to get this data in my form so that is why after reading from the php, i send it back via AJAX
Do you have suggestions guys.
XML adds a lot of extra markup for most ajax requests. If you are expecting some kind of list with data entities, sending them in a JSON format is the way to go.
I used JSON to get quite huge arrays with data.
First of all, JSON is just Javascript Object Notation meaning that the Ajax Request would request a String which will actually be evaluated as a Javascript object.
Some browsers offer support for JSON parsing out of the box. Other need a little help. I've used this little library to parse the responseText in all webapps that I developed and had no problems with it.
Now that you know what JSON is and how to use it, here's how the PHP code would look like.
$response = [
"success" => true, // I like to send a boolean value to indicate if the request
// was valid and ok or if there was any problem.
"records" => [
$dataEntity1, $dataEntit2 //....
]
];
echo json_enconde($response );
Try it and see what it echos. I used the php 5.4 array declaration syntax because it's cool! :)
When requesting the data via Ajax you would do:
var response
,xhr = getAjaxObject(); // XMLHttp or ActiveX or whatever.
xhr.open("POST","your url goes here");
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status==200) {
try {
response = JSON.parse(xhr.responseText);
} catch (err) {
response = {
success : false,
//other error data
};
}
if(response.success) {
//your data should be in response
// response.records should have the dataEntities
console.debug(response.records);
}
}
}
Recap:
JSON parsing needs a little help via JSON2 library
PHP can send maps as JSON
Success boolean is widely used as a "successful/unsuccessful" flag
Also, if you're into jQuery, you can just set the dataType : "json" property in the $.ajax call to receive the JSON response in the success callback.

Categories

Resources