I am trying to grab data by using JSON in ionWordpress (ionic framework plugin) for wordpress.
I'm using the JSON API. Here's the data from my JSON.
{
"status": "ok",
"count": 6,
"count_total": 45,
"pages": 8,
"posts": [
{
"id": 1874,
"type": "event",
"slug": "stesets",
"url": "http://www.web.com/page",
"status": "publish",
"title": "setst",
"title_plain": "tests",
"content": "testsetstst",
"excerpt": "testsetststtes",
"date": "2017-11-27 17:20:17",
"modified": "2017-11-27 17:20:17",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "test",
"name": "test",
"first_name": "",
"last_name": "",
"nickname": "test",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "closed",
"thumbnail": "http://www.website.com/wp-content/uploads/2017/11/image.jpg",
My problem is when I tried to grab the data, it includes the [] as well. For eg.
{{post.thumbnail}}
This would show ["http://www.website.com/wp-content/uploads/2017/11/image.jpg"]
but I just want the link itself without the [" "]
How do I do this?
I used:
{{post.thumbnail[0]}}
as mentioned in comments by Diluk Angelo.
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I'm receiving some JSON POST data from a Webhook into my server. I can get the JSON data as follows:
$orderJSON = file_get_contents('php://input');
which returns this JSON:
{
"order": {
"billing_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"email": "testing#gmail.com",
"first_name": "Hansel",
"last_name": "Gretten",
"phone": "212 554 7855",
"postcode": "2026",
"state": "NSW"
},
"cart_tax": "3.60",
"completed_at": "2016-12-19T11:07:15Z",
"coupon_lines": [],
"created_at": "2016-12-19T11:07:15Z",
"currency": "AUD",
"customer": {
"billing_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"email": "testing#gmail.com",
"first_name": "Hansel",
"last_name": "Gretten",
"phone": "212 554 7855",
"postcode": "2026",
"state": "NSW"
},
"email": "testing#gmail.com",
"first_name": "Hansel",
"id": 0,
"last_name": "Gretten",
"shipping_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"first_name": "Hansel",
"last_name": "Gretten",
"postcode": "2026",
"state": "NSW"
}
},
"fee_lines": [],
"id": 3304,
"is_vat_exempt": false,
"line_items": [
{
"id": 113,
"meta": [],
"name": "Happy Ninja",
"price": "18.00",
"product_id": 37,
"quantity": 2,
"sku": "",
"subtotal": "36.00",
"subtotal_tax": "3.60",
"tax_class": null,
"total": "36.00",
"total_tax": "3.60"
},
{
"id": 114,
"meta": [],
"name": "Water Bottles",
"price": "20.50",
"product_id": 3291,
"quantity": 1,
"sku": "PD885536",
"subtotal": "20.50",
"subtotal_tax": "0.00",
"tax_class": "standard",
"total": "20.50",
"total_tax": "0.00"
}
],
"note": "Call to arrange delivery time",
"order_key": "wc_order_5857bf639d951",
"order_number": 3304,
"payment_details": {
"method_id": "eway",
"method_title": "Credit Card",
"paid": false
},
"shipping_address": {
"address_1": "76 Pacific Drive",
"address_2": "",
"city": "Bondi",
"company": "Hanson Media",
"country": "AU",
"first_name": "Hansel",
"last_name": "Gretten",
"postcode": "2026",
"state": "NSW"
},
"shipping_lines": [
{
"id": 115,
"method_id": "local_pickup:1",
"method_title": "Local Pickup",
"total": "0.00"
}
],
"shipping_methods": "Local Pickup",
"shipping_tax": "0.00",
"status": "pending",
"subtotal": "56.50",
"tax_lines": [
{
"code": "AU-GST-1",
"compound": false,
"id": 116,
"rate_id": "1",
"title": "GST",
"total": "3.60"
}
],
"total": "60.10",
"total_discount": "0.00",
"total_line_items_quantity": 3,
"total_shipping": "0.00",
"total_tax": "3.60",
"updated_at": "2016-12-19T11:07:15Z",
"view_order_url": "https://mywebsite.com/my-account/view-order/3304"
}
}
I now need to get individual elements from the JSON, e.g. I would like to get the id value (3304). I've tried:
$orderID = $orderJSON->id;
and
$orderID = $orderJSON[id];
but this just generates errors like 'Trying to get property of non-object'.
Use the json_decode() function to translate your JSON file into a PHP readable data.
<?php
$json = json_decode($orderJSON);
Once you get your JSON file decoded, you can use the function print_r() to print a nice representation of your datas.
<?php
print_r($orderJSON);
This can help you identify how the file is formed and thus determine all the dimensions you need to display the wanted value.
JSON to nice
Now, if you look closely, you'll see that in order to print your ID, you need to pass through the order dimension.
So you may want to use the syntax $json->order->id to target the wanted ID.
<?php
echo $json->order->id;
Display the ID from JSON
PHP : JSON.
PHP : print_r().
You need to use json_decode() first as I can't see this in your question.
id is under order parent so you need to do sonething like $orderJSON->order->id
You must also check decoded $orderJSON is not empty
Example
$orderJSON = json_decode($orderJSON);
if (empty($orderJSON)) {
throw new RuntimeException('Malformed json');
}
$orderID = $orderJSON->order->id;
I've tried to switch the datasource of the Kendo UI Gantt example inside PHP. I have mapped the schema with what is being returned, but I just get a blank gantt chart with one heading - "undefined".
{
"1": {
"id": "1",
"orderId": "1",
"title": "TESTER1",
"start": "\/new Date('2016-01-01 09:00:00')\/",
"end": "\/new Date('2016-02-01 00:00:00')\/",
"project": "1",
"client": "4218",
"parent": "0",
"percentComplete": "10.11"
},
"2": {
"id": "2",
"orderId": "2",
"title": "TESTER2",
"start": "\/new Date('2016-01-03 09:00:00')\/",
"end": "\/new Date('2016-02-01 00:00:00')\/",
"project": "1",
"client": "4218",
"parent": "0",
"percentComplete": "50.00"
}
}
Above is the JSON being sent back to Kendo, but it doesn't render.
Found the solution:
I type casted the integers, set parents to null rather than zero (0) and converted dates into milliseconds in the PHP layer before putting through to Kendo. I also removed the keys which resulted in the below JSON to be created. This solved my rendering problem.
[{
"id": 1,
"orderId": 1,
"title": "TESTER1",
"start": "\/Date(1463126400000)\/",
"end": "\/Date(1463958000000)\/",
"project": 1,
"client": 4218,
"parent": null,
"percentComplete": 10
}, {
"id": 2,
"orderId": 2,
"title": "TESTER2",
"start": "\/Date(1463990400000)\/",
"end": "\/Date(1464130800000)\/",
"project": 1,
"client": 4218,
"parent": null,
"percentComplete": 50
}]
I am trying to extract a segment from the json file of Rome2Rio API with PHP but I cant get an output.
The json file from rome2rio:
{
"serveTime": 1,
"places": [
{ "kind": "town", "name": "Kozani", "longName": "Kozani, Greece", "pos": "40.29892,21.7972", "countryCode": "GR", "regionCode": "ESYE13" },
{ "kind": "city", "name": "Thessaloniki", "longName": "Thessaloniki, Greece", "pos": "40.64032,22.93527", "countryCode": "GR", "regionCode": "ESYE12" }
],
"airports": [],
"airlines": [],
"aircrafts": [],
"agencies": [{
"code": "KTEL",
"name": "KTEL",
"url": "http://www.ktelbus.com/?module=default\u0026pages_id=15\u0026lang=en",
"iconPath": "/logos/Trains/KTELgr.png",
"iconSize": "27,23",
"iconOffset": "0,0"
}
],
"routes": [
{ "name": "Bus", "distance": 121.04, "duration": 120, "totalTransferDuration": 0, "indicativePrice": { "price": 9, "currency": "EUR", "isFreeTransfer": 0 },
"stops": [
{ "name": "Kozani", "pos": "40.30032,21.79763", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" },
{ "name": "Thessaloniki", "pos": "40.6545,22.90233", "kind": "station", "countryCode": "GR", "timeZone": "Europe/Athens" }
]
The PHP code I wrote is:
$json_rome2rio = file_get_contents("http://free.rome2rio.com/api/1.2/json/Search?key=&oName=kozani&dName=thessaloniki");
$parsed_json_r = json_decode($json_rome2rio);
echo $parsed_json_r->agencies->name;
The agencies property contains an array of agencies (note the square brackets). To access the name as you're after, you can do the following:
echo $parsed_json_r->agencies[0]->name;
This assumes that at least one agency is returned and that the agency you are after is the first one if more than one is returned.
I create a mobile application for my site in static html, now i want use ajax to take my posts, pages, titles, images, etc.. from my wordpress original site.
For this, the best solution i think is use the JSON API plugin -> https://wordpress.org/plugins/json-api/
I already installed, and it's working good in the pages, except for the main page, it's not sending the right thinks for my mobile app.
Anyone knows what is the problem? This is a problem with my wordpress site or maybe a plugin problem?
It's sending this:
{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [
{
"id": 1689,
"type": "post",
"slug": "teste",
"url": "http://myurlsite.com/teste/",
"status": "publish",
"title": "Teste",
"title_plain": "Teste",
"content": "<p>[123-contact-form i523156]</p>\n",
"excerpt": "<p>[123-contact-form i523156]</p>\n",
"date": "2013-12-10 20:11:14",
"modified": "2013-12-10 20:11:14",
"categories": [
{
"id": 1,
"slug": "sem-categoria",
"title": "Sem categoria",
"description": "",
"parent": 0,
"post_count": 1
}
],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "Administrador",
"last_name": "Wemaster",
"nickname": "admin",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "open",
"custom_fields": {
"cro_sidebar": [
"1"
],
"cro_sidebarsel": [
"0"
],
"cro_readmore": [
"1"
],
"cro_showpromo": [
"1"
]
}
}
]
}
If u observe, it's returning a post, not a page:
"type": "post",
"slug": "teste",
"url": "http://myurlsite.com/teste/",
"status": "publish",
"title": "Teste",
"title_plain": "Teste",
I delete the test post and now it~s returning this:
{"status":"ok","count":0,"count_total":0,"pages":0,"posts":[]}
Try accessing the post via this url:
http://www.yourdomain.com/api/get_page/?slug=<page-slug>
or use the page id instead:
http://www.yourdomain.com/api/get_page/?id=<page-id>
I've tested both and both work with that plug-in.
For more information read this information by the developer(s).
I am managed to get tokens and using token to get the content from box.com site. I seen with other site like drop box,smug mug gives many versions of URL of images but here I am not seeing when I pull using the below command
https://www.box.com/api/2.0/folders/0?access_token= and the result is below.
I want to know the path of an image 20131228_181031.jpg in below data, I want to pull the image using php file_getcontent command for which I need image path.
{
"type": "folder",
"id": "0",
"sequence_id": null,
"etag": null,
"name": "All Files",
"created_at": null,
"modified_at": null,
"description": "",
"size": 9985219,
"path_collection": {
"total_count": 0,
"entries": []
},
"created_by": {
"type": "user",
"id": "",
"name": "",
"login": ""
},
"modified_by": {
"type": "user",
"id": "207866808",
"name": "praveen",
"login": " my id"
},
"trashed_at": null,
"purged_at": null,
"content_created_at": null,
"content_modified_at": null,
"owned_by": {
"type": "user",
"id": "207866808",
"name": "Chandler",
"login": "email#tbl.com"
},
"shared_link": null,
"folder_upload_email": null,
"parent": null,
"item_status": "active",
"item_collection": {
"total_count": 5,
"entries": [
{
"type": "file",
"id": "12673472942",
"sequence_id": "0",
"etag": "0",
"sha1": "a43eceb5de8ea1334aa545c95e92d7527f7bf163",
"name": "20131228_181031.jpg"
},
{
"type": "file",
"id": "12673467202",
"sequence_id": "0",
"etag": "0",
"sha1": "dfce2896cd97856fbe2755ec5b7e344103181e87",
"name": "20131228_181034.jpg"
},
{
"type": "file",
"id": "12673477676",
"sequence_id": "0",
"etag": "0",
"sha1": "dee70d192fc6bfec538d5581f8460005d7a79155",
"name": "20131228_181938.jpg"
},
{
"type": "file",
"id": "12673481562",
"sequence_id": "0",
"etag": "0",
"sha1": "a07e7c970ed0aa7fdab955aaad0d4e245d1595cd",
"name": "20131228_181943.jpg"
},
{
"type": "file",
"id": "12673486582",
"sequence_id": "0",
"etag": "0",
"sha1": "156446b911a22604b2a0c032888b4d7a6b6a3bfd",
"name": "20131228_181957.jpg"
}
],
"offset": 0,
"limit": 100,
"order": [
{
"by": "type",
"direction": "ASC"
},
{
"by": "name",
"direction": "ASC"
}
]
}
}
On the Box API, when requesting a file, you do not need to know the folder it is in and so you do not need to build the path. You just make the request with the file id and include your access token in the header. For more info see the getting started docs here.
A quick example using file_get_contents is below:
<?php
$fileId = 12673472942;
$accessToken = 'YOURACCESSTOKENGOESHERE';
$sBox = stream_context_create(array(
'http'=> array(
'header' => "Authorization: Bearer $accessToken\r\n"
)
));
$fileData = file_get_contents(
"https://api.box.com/2.0/files/$fileId/content",
false,
$sBox
);
var_dump($fileData);
The $fileId is the "id" field in the JSON data you provided, so for the file you need it would be 12673472942.
Hope that helps.