I'm very new to the world of APIs and am currently trying to display all reviews I have on my reviews.io account for a specific product. I've tried to make the following script, which now seems to work, however, I would love to have the results sorted nicely in a for-each loop.
Here my current script:
<?php
$header_data = array(
'store' => 'MYSHOP',
'apikey' => 'MYKEY',
'method' => 'GET'
);
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => 'https://api.reviews.co.uk/product/review?store=MYSHOP&sku=TSCB20&apikey=MYKEY',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => false
);
curl_setopt_array($ch, $curlOpts);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
$rv = curl_exec($ch);
$data = json_decode($rv,true);
curl_close($ch);
?>
This outputs all reviews in Raw code or JSON I believe, however, I would like to have them displayed nicely in a div for each item.
The output is as follows (formatted):
{
"store": {
"name": "",
"logo": ""
},
"stats": {
"average": "5.0000",
"count": 2
},
"reviews": {
"total": 2,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"from": 1,
"to": 2,
"data": [
{
"votes": null,
"flags": null,
"title": "David",
"product_review_id": 2978412,
"review": "...is beautiful and I had a great time on this tour. Already started to plan my next trip with them",
"sku": "TSCB20",
"rating": 5,
"date_created": "",
"order_id": "",
"timeago": "",
"reviewer": {
"first_name": "David",
"last_name": "",
"verified_buyer": "yes",
"address": "",
"profile_picture": "",
"gravatar": "38677a4e8a55189055d6e5bf2efa9ade"
},
"ratings": [],
"replies": [],
"images": [],
"product": {
"sku": "TSCB20",
"name": "",
"description": "",
"link": "",
"image_url": "",
"mpn": "",
"brand": null,
"category": null,
"custom": null
},
"author": {
"email": "david#"
}
},
{
"votes": null,
"flags": null,
"title": null,
"product_review_id": 2978009,
"review": "Exceeded expectations on all fronts cultural and Food ",
"sku": "TSCB20",
"rating": 5,
"date_created": "",
"order_id": "RGFY4ZG",
"timeago": "",
"reviewer": {
"first_name": "Alan",
"last_name": "",
"verified_buyer": "yes",
"address": "",
"profile_picture": "",
"gravatar": "64e2ac644a158b76a82f9e1c5c2886f5"
},
"ratings": [],
"replies": [],
"images": [],
"product": {
"sku": "TSCB20",
"name": "",
"description": "",
"link": "",
"image_url": "",
"mpn": "",
"brand": null,
"category": null,
"custom": null
},
"author": {
"email": ""
}
}
]
},
"ratings": [],
"settings": {
"write_review_button": 1,
"disable_product_seo_css": 0,
"show_product_review_titles": 0
},
"word": "Excellent",
"products": [
{
"sku": "TSCB20",
"name": ""
}
],
"write_review_link": ""
}
My ideal outcome now would be a loop, that fetches the reviewer's name, the review itself and the star-rating. e.g.
<p class="review">Review here</p>
<p class="reviewer">Name here</p>
<p class="rating">Rating here</p>
So technically based on the current output, I should have 2 reviews. Some expert help would be greatly appreciated as I am very new to APIs. Thank you very much in advance.
Simple foreach() will do the job:
<?php foreach($data['reviews']['data'] as $dat){?>
<p class="review"><?php echo $dat['review'];?></p>
<p class="reviewer"><?php echo trim($dat['reviewer']['first_name'].' '.$dat['reviewer']['last_name']);?></p>
<p class="rating"><?php echo $dat['rating'];?></p>
<?php }?>
I am using php wrapper, try create an item, all is ok, item is creating, but I can't change status, tried different ways, but can't find right way.
Need change status to "Closed" - http://prntscr.com/fsrwc3
Codes not works:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
or
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
$get_item = PodioItem::get_basic($item->item_id);
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
After create item (after new PodioItem...), if I get fields after this code (just get $item->fields or PodioItem::get_basic...), I can see correct status, only in the code, only immediatly after create item, but if I get this item in the another code (just PodioItem::get_basic...) I will see default value, so code don't change status, looks like I just see some cache.
If I create item on the one script:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>13]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
Then, in the another script update field, it will be change:
$get_item = PodioItem::get_basic('639637317');
$get_item->fields['status']->values = ['id'=>13];
$get_item->save();
Update - debug information:
Get log by test code -
PodioItem::create($app_id, ['fields' => ['status'=>[13], 'category'=>[3], 'contract-type'=>[4]]]);
(simple variant for creating an item, have the same problems like new PodioItem... )
All fields have category type, but:
status - have inline show type - don't chnages
category - have dropdown show type - Is changing
contract-type - have dropdown show type - Is changing
Log:
2017-07-08 11:07:22 200 POST /item/app/12152727/
2017-07-08 11:07:22 Request body: {"fields":{"status":[8],"category":[3],"contract-type":[4]}}
2017-07-08 11:07:22 Reponse: {
"presence": {
"ref_type": "item",
"ref_id": 641331142,
"user_id": 4194774,
"signature": "c165b85090e6ad28e74ae4baf93ee56113f88bc9"
},
"app": {
"status": "active",
"sharefile_vault_url": null,
"name": "Projects",
"default_view_id": null,
"url_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"icon_id": 378,
"link_add": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/new",
"app_id": 12152727,
"current_revision": 141,
"item_name": "Project",
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects",
"url_label": "projects",
"config": {
"item_name": "Project",
"icon_id": 378,
"type": "standard",
"name": "Projects",
"icon": "378.png"
},
"space_id": 3466816,
"icon": "378.png"
},
"created_on": "2017-07-10 15:31:16",
"last_event_on": "2017-07-10 15:31:16",
"linked_account_data": null,
"sharefile_vault_folder_id": null,
"app_item_id_formatted": "3988",
"recurrence": null,
"title": "ACSC",
"participants": {},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"priority": 641331142.0,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"subscribed_count": 1,
"reminder": null,
"ref": null,
"revision": 0,
"app_item_id": 3988,
"link": "https:\/\/podio.com\/acs-1com\/project-management\/apps\/projects\/items\/3988",
"item_id": 641331142,
"sharefile_vault_url": null,
"rights": ["subscribe", "grant", "add_conversation", "rate", "update", "delete", "add_file", "grant_view", "view", "comment", "add_task"],
"fields": [{
"status": "active",
"type": "category",
"field_id": 93352415,
"label": "Division",
"values": [{
"value": {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Division",
"visible": true,
"delta": 5,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "ACSE",
"id": 1,
"color": "DCEBD8"
}, {
"status": "active",
"text": "ACSB",
"id": 2,
"color": "F7F0C5"
}, {
"status": "active",
"text": "ACSC",
"id": 3,
"color": "D2E4EB"
}],
"display": "dropdown"
}
},
"external_id": "category"
}, {
"status": "active",
"type": "category",
"field_id": 148215928,
"label": "Contract Type",
"values": [{
"value": {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Contract Type",
"visible": true,
"delta": 7,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "active",
"text": "PO \/ Purchase \/ T&M",
"id": 1,
"color": "FFD5C2"
}, {
"status": "active",
"text": "Original Contract",
"id": 2,
"color": "D2E4EB"
}, {
"status": "active",
"text": "Service Rider",
"id": 3,
"color": "DCEBD8"
}, {
"status": "active",
"text": "No Contract",
"id": 4,
"color": "DDDDDD"
}],
"display": "dropdown"
}
},
"external_id": "contract-type"
}, {
"status": "active",
"type": "category",
"field_id": 93034840,
"label": "Instal Status",
"values": [{
"value": {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}
}],
"config": {
"default_value": null,
"unique": false,
"description": null,
"hidden_create_view_edit": false,
"required": true,
"mapping": null,
"label": "Instal Status",
"visible": true,
"delta": 13,
"hidden": false,
"settings": {
"multiple": false,
"options": [{
"status": "deleted",
"text": "To Be Reviewed",
"id": 2,
"color": "F7F0C5"
}, {
"status": "deleted",
"text": "ACS Accounting Review",
"id": 5,
"color": "FFD5C2"
}, {
"status": "deleted",
"text": "Ignite Setup",
"id": 10,
"color": "DCEBD8"
}, {
"status": "active",
"text": "To Be Scheduled",
"id": 8,
"color": "D2E4EB"
}, {
"status": "active",
"text": "In Progress",
"id": 6,
"color": "DCEBD8"
}, {
"status": "active",
"text": "Warranty",
"id": 11,
"color": "F7F0C5"
}, {
"status": "active",
"text": "Complete",
"id": 3,
"color": "D1F3EC"
}, {
"status": "deleted",
"text": "Closed",
"id": 7,
"color": "DDDDDD"
}, {
"status": "active",
"text": "Cancelled",
"id": 9,
"color": "DDDDDD"
}, {
"status": "deleted",
"text": "VA in Progress",
"id": 4,
"color": "E1D8ED"
}, {
"status": "deleted",
"text": "Submitted",
"id": 1,
"color": "F7F0C5"
}, {
"status": "active",
"text": "On Hold",
"id": 12,
"color": "F7D1D0"
}, {
"status": "active",
"text": "Closed",
"id": 13,
"color": "E1D8ED"
}],
"display": "inline"
}
},
"external_id": "status"
}],
"initial_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"current_revision": {
"item_revision_id": 1664054437,
"created_via": {
"url": null,
"auth_client_id": 25162,
"display": true,
"name": "importer",
"id": 25162
},
"created_by": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"created_on": "2017-07-10 15:31:16",
"user": {
"user_id": 4194774,
"name": "Anton Mikhailov",
"url": "https:\/\/podio.com\/users\/4194774",
"type": "user",
"image": null,
"avatar_type": "file",
"avatar": null,
"id": 4194774,
"avatar_id": null,
"last_seen_on": "2017-07-10 15:31:15"
},
"type": "creation",
"revision": 0
},
"linked_account_id": null,
"push": {
"timestamp": 1499700676,
"expires_in": 21600,
"channel": "\/item\/641331142",
"signature": "b8a816ff367da6bc730071c875ca3fdca2d2c344"
},
"external_id": null
}
Have you tried http://podio.github.io/podio-php/fields/#category-field ?
Setting values
Set a single value by using the option_id. You can also
add a value with add_value()
$item = PodioItem::get_basic(123);
$field_id = 'category';
// Set value to a single option
$item->fields[$field_id]->values = 4; // option_id=4
// Add value to existing selection
$item->fields[$field_id]->add_value(4); // option_id=4
Use an array to set multiple values
$item = PodioItem::get_basic(123);
$field_id = 'category';
$item->fields[$field_id]->values = array(4,5,6); // option_ids: 4, 5 and 6
Creating item with value:
$fields = new PodioItemFieldCollection([
new PodioCategoryItemField(['external_id'=>'status', 'values'=>array(13)]),
]);
$item = new PodioItem([
'app' => new PodioApp($app_id),
'fields' => $fields
]);
$item->save();
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've gone through a few examples and documentations and kind find a solution update a nested object in the this result set.
I can add one (if one does not exist)
I can append to it (if one does exist)
Can't figure out how to delete a selected entry.
Is there a method I can use (using the php client) to add an entry if it does not exist / update an entry if it does exist / delete the second entry.
I'm inheriting this problem and am new to Elastic search.
Thanks.
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "products",
"_type": "categories",
"_id": "AUpRjtKZfXI7LIe9OpNx",
"_score": 1,
"_source": {
"name": "Primary",
"description": "Primary Category",
"slug": "Primary",
"created": "2014-12-16 00:25:22",
"parent": [
{
"name": "First One",
"description": "Test",
"id": "ae74ea4e2e865ed3fd60c18a06e69c65",
"slug": "first-one"
},
{
"name": "Second One",
"description": "Testing Again",
"id": "c8dbe5143c8dfd6957fa33e6cea7a0a8",
"slug": "second-one"
}
]
}
}
]
}
}
Do you want to do all three in the same operation?
Deleting the second nested object is achieved through a script which removes the second element:
PUT /products
{
"mappings": {
"categories": {
"properties": {
"parent": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"id": { "type": "string", "index": "not_analyzed" },
"slug": { "type": "string" }
}
}
}
}
}
}
PUT /products/categories/1
{
"name": "Primary",
"description": "Primary Category",
"slug": "Primary",
"created": "2014-12-16 00:25:22",
"parent": [
{
"name": "First One",
"description": "Test",
"id": "ae74ea4e2e865ed3fd60c18a06e69c65",
"slug": "first-one"
},
{
"name": "Second One",
"description": "Testing Again",
"id": "c8dbe5143c8dfd6957fa33e6cea7a0a8",
"slug": "second-one"
}
]
}
POST /products/categories/1/_update
{
"script" : "ctx._source.parent.remove(1)",
"lang": "groovy"
}
GET /products/categories/1
So in PHP code (using the official PHP client), the update would look like:
$params = [
'index' => 'products',
'type' => 'categories',
'id' => 1,
'body' => [
'script' => 'ctx._source.parent.remove(1)',
'lang' => 'groovy'
]
];
$result = $client->update($params);