My JSON:
{"data":{"addresses":{"bitcoincash":"qzx3k8cq2e66k4glnt2derr5mppzc6xmvuxgsyp778","bitcoin":"1GjKuo1Q9sw8NytE31J5RPnVpYpEzp47hu","ethereum":"0xd7410e84e9c336937637e3cb472ad112c258ede3","litecoin":"LiQCBwuvW4RVuAg2dBNzS4fkviDwi8EBKa"},"code":"PGVD745Y","created_at":"2018-08-18T04:26:23Z","description":"dddd","expires_at":"2018-08-18T05:26:23Z","hosted_url":"https://commerce.coinbase.com/charges/example","id":"ddd","metadata":{"customer_id":"IuYBD5X7ylEV6g0xyTWi","name":"Guest#localhost.com"},"name":"ddd","payments":[],"pricing":{"local":{"amount":"19.85","currency":"USD"},"ethereum":{"amount":"0.063584000","currency":"ETH"},"bitcoin":{"amount":"0.00303719","currency":"BTC"},"bitcoincash":{"amount":"0.03345637","currency":"BCH"},"litecoin":{"amount":"0.32861518","currency":"LTC"}},"pricing_type":"fixed_price","resource":"charge","timeline":[{"status":"NEW","time":"2018-08-18T04:26:23Z"}]}}
My PHP:
$exec = json_encode($exec);
$json = json_decode($exec, TRUE);
echo $json['hosted_url'];
It just returns { always, in fact even if I put $json['safasfsaf'] it would still return {
What is the issue, the JSON is valid?
You can get hosted_url in this way (ERROR: you missing data $json['data']['hosted_url'])
also you can check your desired output here
<?php
$a = '{
"data": {
"addresses": {
"bitcoincash": "qzx3k8cq2e66k4glnt2derr5mppzc6xmvuxgsyp778",
"bitcoin": "1GjKuo1Q9sw8NytE31J5RPnVpYpEzp47hu",
"ethereum": "0xd7410e84e9c336937637e3cb472ad112c258ede3",
"litecoin": "LiQCBwuvW4RVuAg2dBNzS4fkviDwi8EBKa"
},
"code": "PGVD745Y",
"created_at": "2018-08-18T04:26:23Z",
"description": "dddd",
"expires_at": "2018-08-18T05:26:23Z",
"hosted_url": "https://commerce.coinbase.com/charges/example",
"id": "ddd",
"metadata": {
"customer_id": "IuYBD5X7ylEV6g0xyTWi",
"name": "Guest#localhost.com"
},
"name": "ddd",
"payments": [],
"pricing": {
"local": {
"amount": "19.85",
"currency": "USD"
},
"ethereum": {
"amount": "0.063584000",
"currency": "ETH"
},
"bitcoin": {
"amount": "0.00303719",
"currency": "BTC"
},
"bitcoincash": {
"amount": "0.03345637",
"currency": "BCH"
},
"litecoin": {
"amount": "0.32861518",
"currency": "LTC"
}
},
"pricing_type": "fixed_price",
"resource": "charge",
"timeline": [{
"status": "NEW",
"time": "2018-08-18T04:26:23Z"
}]
}
}';
$json = json_decode($a, TRUE);
echo "<pre>";
print_r($json['data']['hosted_url']);
You have error reporting turned off
This error is hidden for you
Warning: Illegal string offset 'hosted_url'
You can turn error reporting on with this code
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
For your code you need to replace $json['hosted_url'] with $json['data']['hosted_url']
$exec = getJson();
$json = json_decode($exec, TRUE);
echo $json['data']['hosted_url'];
Also response is already json so you must not json_encode it
I have noticed that, you are decode json data two time, so you are getting errors.
Hi, i have tried this way.
$exe = '{"data":{"addresses":{"bitcoincash":"qzx3k8cq2e66k4glnt2derr5mppzc6xmvuxgsyp778","bitcoin":"1GjKuo1Q9sw8NytE31J5RPnVpYpEzp47hu","ethereum":"0xd7410e84e9c336937637e3cb472ad112c258ede3","litecoin":"LiQCBwuvW4RVuAg2dBNzS4fkviDwi8EBKa"},"code":"PGVD745Y","created_at":"2018-08-18T04:26:23Z","description":"dddd","expires_at":"2018-08-18T05:26:23Z","hosted_url":"https://commerce.coinbase.com/charges/example","id":"ddd","metadata":{"customer_id":"IuYBD5X7ylEV6g0xyTWi","name":"Guest#localhost.com"},"name":"ddd","payments":[],"pricing":{"local":{"amount":"19.85","currency":"USD"},"ethereum":{"amount":"0.063584000","currency":"ETH"},"bitcoin":{"amount":"0.00303719","currency":"BTC"},"bitcoincash":{"amount":"0.03345637","currency":"BCH"},"litecoin":{"amount":"0.32861518","currency":"LTC"}},"pricing_type":"fixed_price","resource":"charge","timeline":[{"status":"NEW","time":"2018-08-18T04:26:23Z"}]}}';
$data = json_decode($exe, TRUE);
echo $data['data']['hosted_url'];
Related
I have this json code
"result": [
{
"update_id": 74783732,
"message": {
"message_id": 852,
"from": {
"id": ---,
"is_bot": false,
"first_name": "---",
"username": "---",
"language_code": "en"
},
"chat": {
"id": ---,
"first_name": "---",
"username": "---",
"type": "private"
},
"date": 1646306224,
"text": "#username",
"entities": [
{
"offset": 0,
"length": 16,
"type": "mention"
}
]
}
}
]
I can get content from update_id , message, first_name etc.
but I want to get "mention" from type how can i do it?
my code is
here i decode json and get arrays from json and put them in variable and use it in my queries but I cant get mention from entities...
$update = file_get_contents("php://input");
$update_array = json_decode($update, true);
if( isset($update_array["message"]) )
{
$text = $update_array["message"]["text"];
$chat_id = $update_array["message"]["chat"]["id"];
}
if(isset($update_array["message"]["entities"]["type"]) and $update_array=="mention")
{
$get_username = $text;
show_users_by_username($get_username);
}
tnx for helping
$update_array will never be equal to "mention" but $update_array["message"]["entities"]["type"] yes.
Change your condition.
I have my json from a url feed. Here's a sample below. I'm not doing the foreach loop correctly is the problem
{
"useLive": true,
"models": [
{
"snapshotUrl": "https://img-eu.whatevercdn.com/eu7/previews/1537971705/5293074",
"widgetPreviewUrl": "https://img-eu.whatevercdn.com/eu7/previews/1537971705/5293074",
"id": 5293074,
"country": "",
"gender": "female",
"isNew": false,
"previewUrl": "https://st.whatevercdn.com/cdn/previews/b/a/a/baa515a42e75d80b0dc1e7a75bf4ea0f-full",
"previewUrlThumbBig": "https://st.whatevercdn.com/cdn/previews/b/a/a/baa515a42e75d80b0dc1e7a75bf4ea0f-thumb-big",
"previewUrlThumbSmall": "https://st.whatevercdn.com/cdn/previews/b/a/a/baa515a42e75d80b0dc1e7a75bf4ea0f-thumb-small",
"broadcastGender": "female",
"snapshotServer": "eu7",
"tags": ["autoTagPopular","keyword","keyword2"],
"topBestPlace": 0,
"username": "model1",
"languages": ["en"],
"stripScore": 998.5,
"token": "93021860dbebd5ba27e604f6b4b93754"
},
{
"snapshotUrl": "https://img-eu.whatevercdn.com/eu8/previews/1537971700/6492104",
"widgetPreviewUrl": "https://img-eu.whatevercdn.com/eu8/previews/1537971700/6492104",
"id": 6492104,
"country": "",
"gender": "female",
"isNew": false,
"previewUrl": "https://st.whatevercdn.com/cdn/previews/2/b/3/2b366955f5a66d73ee038d43bf77c99b-full",
"previewUrlThumbBig": "https://st.whatevercdn.com/cdn/previews/2/b/3/2b366955f5a66d73ee038d43bf77c99b-thumb-big",
"previewUrlThumbSmall": "https://st.whatevercdn.com/cdn/previews/2/b/3/2b366955f5a66d73ee038d43bf77c99b-thumb-small",
"broadcastGender": "female",
"snapshotServer": "eu8",
"tags": ["autoTagPopular","keyword","keyword2"],
"topBestPlace": 0,
"username": "model2",
"languages": [],
"stripScore": 997.25,
"token": "2c6ee95270f6faf76cd33321732136e3"
}
],
"ttl": 15,
"tagType": "F+T",
"tagName": "Featured",
"defaultTags": [
{
"name": "whatever1",
"url": "/tags/whatever1"
},
{
"name": "whatever2",
"url": "/tags/whatever2"
},
{
"name": "whatever3",
"url": "/tags/whatever3"
}
],
"serverTime": "2018-09-26T14:23:00Z"
}
Here's my php code so far. I've tried quite a few different things. I normally use xml feeds which seem to be easy for me to setup for what I need. I'm not sure what I'm missing here.
$url = 'https://whatever.com/api/external/v4/widget?userId=whatever&tag=featured'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$performers = json_decode($data, true); // decode the JSON feed
foreach ($performers as $performer) {
$info = $performer[0]["username"];
echo $info;
}
I'm only getting the first username and then error messages.
Warning: Illegal string offset 'username' in /whatever
Can anyone help with this?
You should use $performers['models'] array in foreach and then get username it will work fine try the following code
$performers = json_decode($data, true);
if(isset($performers['models'])){
foreach ($performers['models'] as $performer) {
$info = (isset($performer["username"])) ? $performer["username"] : '';
echo $info;
echo "<br>";
}
}
Output
model1
model2
This is my php file code where i am fetching data from thecatapi
i needed the specific format for datatable
$xml=file_get_contents('https://thecatapi.com/api/images/get?format=xml&results_per_page=3');
$xml = new SimpleXMLElement($xml);
$datas=array();
$datas['rows'] = $xml->data->images;
echo json_encode($datas, true);
Output is:
{"total":50,
"rows":{"image":[
{"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=e0i",
"id":"e0i",
"source_url":"http:\/\/thecatapi.com\/?id=e0i"
},
{"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=MTYwNDE0MQ",
"id":"MTYwNDE0MQ",
"source_url":"http:\/\/thecatapi.com\/?id=MTYwNDE0MQ"
},{
"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=bon",
"id":"bon",
"source_url":"http:\/\/thecatapi.com\/?id=bon"
}
]
}
}
but i wanted in this json form
{
"total": 800,
"rows": [
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
Just came up with an alternativ solution. Change this line:
$datas['rows'] = $xml->data->images;
to this:
$datas['rows'] = $xml->data->images->image;
otherwise you can still do this
Decode the json string into an object and modify it suit your needs.
$stdClassObject = json_decode($jsonData);
$stdClassObject->rows = $stdClassObject->rows->image;
$newJsonData = json_encode($stdClassObject, JSON_PRETTY_PRINT);
echo '<pre>';
print_r($newJsonData);
echo '</pre>';
New output:
{
"total": 50,
"rows": [
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=e0i",
"id": "e0i",
"source_url": "http:\/\/thecatapi.com\/?id=e0i"
},
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=MTYwNDE0MQ",
"id": "MTYwNDE0MQ",
"source_url": "http:\/\/thecatapi.com\/?id=MTYwNDE0MQ"
},
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=bon",
"id": "bon",
"source_url": "http:\/\/thecatapi.com\/?id=bon"
}
]
}
I am facing with a problem, when I am trying to parse json returns from server into array in php. Here is my code ...
<?php
mb_internal_encoding('UTF-8');
$url = 'http://localhost/busexpress/api/v1/mobile_user_register/mobile_user_register/retrieve.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
//$data="'".$data."'";
echo $data;
curl_close($ch);
//$trimspace = preg_replace('/\s+/', '', $data);
//echo $trimspace;
$jdata = json_decode($data, true);
print_r $jdata;
?>
This is the json after trimming space. I also want to convert it int array with json_decode() but no result return. I think this json is valid. And suggestion pls. This is my firstly trying to feed web service from server.
Thanks
'{
"status": "1",
"user": [
{
"id": "27",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "0d08ed",
"deposit": "0",
"created": "2015-06-0316:35:08",
"updated": "1110-11-3000:00:00",
"status": "0"
},
{
"id": "28",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "fb4876",
"deposit": "0",
"created": "2015-06-0316:37:14",
"updated": "1000-01-0100:00:00",
"status": "0"
}
]
}'
----------Edit---------
As your suggestion I comment trimming space and correct json format. And echo $data; .....
{
"status": "1",
"user": [
{
"id": "27",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "0d08ed",
"deposit": "0",
"created": "2015-06-0316:35:08",
"updated": "1110-11-3000:00:00",
"status": "0"
},
{
"id": "28",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "fb4876",
"deposit": "0",
"created": "2015-06-0316:37:14",
"updated": "1000-01-0100:00:00",
"status": "0"
}
]
}
In decoding array doesn't have any data.
$jdata = json_decode($data, true);
print_r $jdata;
echo "user status -> ". $jdata["status"];
when I copy that json and hard code in a string, decode it again, it works for me. please see my testing code....
$data =' {"status":"1","mobile_user":[{"id":"1","name":"saa","phone_no":"09978784963","activate_code":"","deposit":"0","created":"2015-05-29 00:00:00","updated":"0000-00-00 00:00:00","status":"1"},{"id":"3","name":"ttr","phone_no":"090930499","activate_code":"","deposit":"0","created":"2015-06-01 00:00:00","updated":"0000-00-00 00:00:00","status":"0"}]}';
$data = json_decode($data,true);
$status = $data['status'];
$mobile_user = $data['mobile_user'];
$id = $mobile_user[0]["id"];
$name = $mobile_user[0]["name"];
echo "id -> ". $id ."<br>";
echo "name -> ". $name;
Any suggestion pls!
I think your json is malformed. Remove $data="'".$data."'";
You can check json error if any.
And $trimspace = preg_replace('/\s+/', '', $data); is needless.
json_decode usually returns an object, so I don't think your code is wrong here.
$arrayObject = new ArrayObject($object);
$array = $arrayObject->getArrayCopy();
This is how you can convert it to an array. It works in PHP 5.3+
Try this
$jdata = json_decode($trimspace, true);
print_r($jdata);
First of all your json is malformed. Remove the '' from the beginning and the end of your file. The contents of $data should look like this:
{
"status": "1",
"user": [
{
"id": "27",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "0d08ed",
"deposit": "0",
"created": "2015-06-0316:35:08",
"updated": "1110-11-3000:00:00",
"status": "0"
},
{
"id": "28",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "fb4876",
"deposit": "0",
"created": "2015-06-0316:37:14",
"updated": "1000-01-0100:00:00",
"status": "0"
}
]
}
Second $jdata is an associative array. You cannot print its contents with echo. Instead do
print_r($jdata);
Third you don't need to remove spaces. Do that in the script that produces the json, otherwise just parse the json with the spaces directly.
How can I read several values with the same name from a JSON from Facebook, when I use this code:
$urlamandar = 'https://graph.facebook.com/'.$user."?access_token=".$access_token;
$content = file_get_contents($urlamandar);
$obj = json_decode($content, true);
$obj = json_decode($content);
It displays this:
{
"id": "XXXXXXXX",
"name": "Ricardo Capistran",
"first_name": "Ricardo",
"last_name": "Capistran",
"gender": "male",
"locale": "es_MX",
"username": "richycapy"
}
And since because there is only one ID, one NAME, one USERNAME, etc, Its quite simple place them in vars like so:
$fbuserid = $obj->{'id'};
$fbname = $obj->{'name'};
$fbusername = $obj->{'username'};
$fbemail = $obj->{'email'};
$fbbirthday = $obj->{'birthday'};
But when it comes to larger files like this code:
$urlamandar = 'https://graph.facebook.com/'.$user.'/albums?access_token='.$access_token;
Its displays a way bigger array like so:
{
"data": [
{
"id": "10150732237818223",
"from": {
"name": "Ricardo Capistran",
"id": "XXXXXXX"
},
"name": "EnterateNorte.com Photos",
"link": "https://www.facebook.com/album.php?fbid=10150732237818223&id=743158222&aid=457026",
"privacy": "custom",
"count": 31,
"type": "app",
"created_time": "2012-03-11T02:44:42+0000",
"updated_time": "2014-01-07T03:13:24+0000",
"can_upload": false
},
{
"id": "440168313222",
"from": {
"name": "Ricardo Capistran",
"id": "743158222"
},
"name": "Timeline Photos",
"link": "https://www.facebook.com/album.php?fbid=440168313222&id=743158222&aid=220377",
"cover_photo": "10151730849598223",
"privacy": "everyone",
"count": 175,
"type": "wall",
"created_time": "2010-06-30T22:38:45+0000",
"updated_time": "2014-01-01T02:09:11+0000",
"can_upload": false
},
{
"id": "10150797320378223",
"from": {
"name": "Ricardo Capistran",
"id": "743158222"
},
"name": "Instagram Photos",
"link": "https://www.facebook.com/album.php?fbid=10150797320378223&id=743158222&aid=466555",
"cover_photo": "10151695050098223",
"privacy": "friends",
"count": 37,
"type": "app",
"created_time": "2012-04-09T23:50:08+0000",
"updated_time": "2013-12-29T08:29:15+0000",
"can_upload": false
}
],
"paging": {
"cursors": {
"after": "NDM1NjY5NjI4MjIy",
"before": "MTAxNTA3MzIyMzc4MTgyMjM="
},
"next": "https://graph.facebook.com/*my_id*/albums?access_token=*access_token*&limit=25&after=NDM1NjY5NjI4MjIy"
}
}
And I would need the “name” and “id” off all the albums, so then I can repeat this same procedure with the containing pictures
Obviously it has way more albums, I just cut it after 3 just to explain my self…
Is there a way to place them in vars? With a php “for each” some how
Thanks!
You can do something like
$obj = json_decode($content, true);
$array_album = array();
foreach($obj['data'] as $key=>$val){
$array_album[] = array("id"=>$val["id"],"name"=>$val["name"]);
echo "ID : ".$val["id"]. " NAME : ".$val["name"] ;
echo "<br />";
}
print_r($array_album); // This will have all the id and names
If you dont want to store in array then the names and id will appear in the above loop and you can do whatever you want with those values. Or use the array $array_album and loop through if you want to use it later.