Data from JSON to php? - 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'];
}
}

Related

JSON API to HTML Koinex API

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

php using file_get_contents with an array

I am trying to pull values from the following url: https://api.binance.com/api/v1/ticker/allPrices
What I want to accomplish:
Obtain all the "symbol" values printed in the website (one under the other)
The result I am looking for, would be:
ETHBTC
LTCBTC
...
This is what I have done so far:
<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
for ($i=0; $i < count($json_data); $i++) {
# code...
print_r($json_data);
}
?>
The result it brings is the entire data (the symbols, prices, plus other irrelevant data).
Thanks in advance.
You can use foreach loop, then echo the value by symbol key
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
foreach ($json_data as $value) {
echo $value["symbol"] . '<br>';
}
This ought to do it:
<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
for ($i=0; $i < count($json_data); $i++) {
echo $json_data[$i]['symbol'] . "\n";
}
?>
json_data["symbol"]
instead in the print

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
}

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; ?>

cutting content from returned array with php

I am trying to cut results out of the RBL data it pulls back.
Here is the code.
<?
$ips = file("list.inc");
foreach($ips as $ip)
{
$rblurl = 'http://rbl-check.org/rbl_api.php?ipaddress=' . trim($ip);
$boom = fopen($rblurl, "r");
$rbl = stream_get_contents($boom);
echo "</br>";
$data = explode(";",$rbl);
print "<pre>";
print_r($data[0]);
print "</pre>";
echo "</br>";
//fclose($boom);
}
?>
This is the result.
emailbasura;bl.emailbasura.org;nowebsite;notlisted
Sorbs;zombie.dnsbl.sorbs.net;nowebsite;notlisted
msrbl;combined.rbl.msrbl.net;nowebsite;notlisted
nixspam;ix.dnsbl.manitu.net;nowebsite;notlisted
Spamcop;bl.spamcop.net;nowebsite;notlisted
I am trying to cut the first part and the last part so it only displays this.
emailbasura notlisted
Sorbs notlisted
msrbl notlisted
nixspam notlisted
Spamcop notlisted
Any help would be great!
first you need to explode the data by the line breaks not just the delimeter:
$data = explode("\n",$rbl);
once you've done that you just echo out the data:
foreach($data as $item) {
$item = explode(';',$item);
echo $item[0].' '.$item[3];
}
foreach($data as $d)
{
$arr_data = explode(';',$d);
$first_data = $arr_data[0];
$last_data = $arr_data[3];
}
Change here
print "<pre>";
print_r($data[0]);
print "</pre>"
as
print "<pre>";
$spl = split(';', $data[0]);
echo $spl[0] . $spl[3];
print "<pre>";

Categories