JSON API to HTML Koinex API - php

This is the link of API https://koinex.in/api/ticker
I need the out put as below
ETH:40000
BTC:5290000
LTC:8500
I have tried this
$json_string = file_get_contents("https://koinex.in/api/ticker");
$parsed_json = json_decode($json_string);
foreach ($parsed_json->{'price'}->{'inr'} as $item) {
$title = $item[0];
$Price = $item[1];
echo "$title \n $Price\n\n";
}
Please help me in this. This is not working.

Use the following code:-
<?php
$json_string = file_get_contents("https://koinex.in/api/ticker");
$parsed_json = json_decode($json_string);
foreach ($parsed_json->prices->inr as $key => $val ){
echo $key.":".$val."\n";
}
?>
Sample OUTPUT:-
ETH:40611
BTC:529000
LTC:8500
XRP:44.19
BCH:71490
OMG:800
REQ:10.24
ZRX:88.01
GNT:42.1
BAT:20.48
AE:241.99
TRX:4.23
XLM:20.7
NEO:3825
GAS:1450
XRB:287
NCASH:1.72
AION:150
EOS:940
CMT:23.47
ONT:499
ZIL:7.97
IOST:3.17
ACT:16.15
ZCO:7.78
POLY:50
ELF:76.13

Related

PHP rest api display an array of values

I am trying to display all the items from an API. How can i display in html or store them in variables. In my code
$api_url = 'demo API';
// Read JSON file
$json_data = file_get_contents($api_url);
// Decode JSON data into PHP array
$response_data = json_decode($json_data, true);
$result = array_values($response_data);
var_dump($result);
but unfortunately i dont know how to display the datas in front
i found a way
<?php
// JSON string
$json_string = '(API)';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
foreach($obj as $row => $innerArray){
foreach($innerArray as $innerRow => $value){
// echo $innerRow.' = '.$value . "<br/>";
if ($innerRow == "thumb_url")
{
?>
<img src="<?php echo $value; ?>" width="280" height="360">
<?php
}
elseif ($innerRow == "title"){
?>
<?php echo $value; ?>
<?php
}
}
}
?>

parse json from url using php and save in mysql db

I've a JSON response from some web service
{
"success":true,
"timestamp":1503291248,
"quotes":{
"SAD":"ABC",
"LOVE":"XYZ",
"FRIENDSHIP":"SOME",
"ENMITY":"LOREM IPSUM",
... //indicates there are a lot more categories
}
}
I have tried to echo data using this script
<?php
$url = 'MY_URL';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach ( $json as $idx=>$json ) {
echo $idx;
}
?>
this prints
successtermsprivacytimestampsourcequotes
but getting empty response, how can I echo/save above josn data in mySql?
<?php
$url = '{
"success":true,
"timestamp":1503291248,
"quotes":{
"SAD":"ABC",
"LOVE":"XYZ",
"FRIENDSHIP":"SOME",
"ENMITY":"LOREM IPSUM",
... //indicates there are a lot more categories
}
}';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach ( $json as $idx=>$json ) {
if(is_array($json))
{
foreach($json as $iidx=>$jjson)
{
echo $iidx.":".$jjson;
}
}
else
{
echo $idx.":".$json;
}
}
$ins_qry = 'INSERT INTO json_table(jsonvalues) values ("'.$json.'")';
$exec_qry = mysqli_query($link,$ins_qry);
?>
This will print json data.
To save this json data in mysql, you can directly insert the $json value into text column of a MySQL table.
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;
Try this..
$url = 'MY_URL';
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach ( $json as $val) {
echo $val['success']; //same for other keys
}

json file_get_contents and show item

I have this code but I can't make the items to show properly, I can only show the data from echo $data sentence but not $item FirstName or Item Bio
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$items = json_decode($data, true);
foreach ($items as $item)
{
$name = $item = ["FirstName"];
$bio = $item = ["Bio"];
}
echo $data
if you see the json response it has 2 elements.
{
message : ...,
department : ....
}
so you have to loop throw $items['department'] rather than $items
and you can access the data like $item['Bio']
So you can change the code like this to get the information
<?php
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965';
$data = file_get_contents($url); // into a variable
$items = json_decode($data, true); // decode the JSON feed
foreach ($items['department'] as $item)
{
$name = $item["FirstName"];
$bio = $item["Bio"];
echo $name . ' ' . $bio . PHP_EOL;
}

Data from JSON to php?

I need help with fetching only attraction data,
i have this php script on my website:
<?php $json_string = 'http://framecreators.nl/efteling/data.php'; $jsondata file_get_content($json_string); $json_o=json_decode(utf8_encode($data)); $attractie = $json_o['Id']['Type']; echo $attractie ?>
and this json data:
http://framecreators.nl/efteling/data.php
I need to convert only a Id and type, ff somebody can help me.
To access the data you're looking for, you'll need to do something like this:
foreach ($json_o['AttractionInfo'] as $a) {
echo $a['Id']; //or whatever you're planning to do with this data
ech0 $a['Type'];
}
<?php
$json_data = file_get_contents('http://framecreators.nl/efteling/data.php');
$data = json_decode($json_data);
$array = [];
foreach($data->AttractionInfo as $item) {
$array['id'][] = $item->Id;
$array['type'][] = $item->Type;
}
echo "<pre>";
print_r($array);
echo "</pre>";
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
foreach($array as $r)
{
echo "ID->".$r['Id'];
echo '';
echo "Type->".$r['Type'];
echo '';
}
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
if(is_array($array))
{
foreach($array as $r)
{
echo $r['Id'];
echo $r['Type'];
}
}

Loop a foreach json loop in another foreach json loop

I'm at a 24 hour hackathon trying to solve this so, excuse if it's a little rushed.
1st for each loop works fine, I'm getting a list of categories from this url
https://dev.xola.com/api/categories
I grab the list with this
$fullurl = "https://dev.xola.com/api/categories";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder
then loop it with this
<?
foreach($json_a as $v)
{?>
echo $v ?}>
Now with the second for each look, I want to grab the items from this url
https://dev.xola.com/api/experiences
that match the category from the last url
so samething
$fullurl = "https://dev.xola.com/api/categories";
$string .= file_get_contents($fullurl); // get json content
$json_b = json_decode($string, true); //json decoder
here's the complete loop I tried
<?
$i=0;
foreach($json_a as $v)
$i++
{?>
echo $v ?
foreach($json_b as $x){?>
if($v==$x):
echo $v
endif;
?>
}?>
This will create a $result array with only the data that had the categories early acquired:
<?php
$categories_url = "https://dev.xola.com/api/categories";
$data = file_get_contents($categories_url);
$categories = json_decode($data, true);
$experiences_url = "https://dev.xola.com/api/experiences";
$data = file_get_contents($experiences_url);
$experiences = json_decode($data, true);
$result = array();
foreach ($experiences['data'] as $experience)
{
if (in_array($experience['category'], $categories))
{
$result[] = $experience;
}
}
print_r($result);
And you can easily read the result with:
foreach ($result as $item)
{
echo $item['category'], "\n";
echo $item['desc'], "\n";
//... other data available ...
}
The data structure of the experiences JSON is not the same as the categories JSON, therefore if($v==$x) will never match. If you want to find all results in experiences with a category from the categories url you can do the following:
<?
$BASE_URL = 'https://dev.xola.com/api/';
$categories = json_decode(file_get_contents($BASE_URL . 'categories'));
$experiences = json_decode(file_get_contents($BASE_URL . 'experiences'));
$matches = array();
foreach( $categories as $category ) {
foreach( $experiences->data as $experience ) {
if( $experience->category === $category ) {
$matches[] = $experience;
}
}
}
?>
<? foreach( $matches as $match ) : ?>
<? echo $match->category; ?><br>
<? endforeach; ?>

Categories