Here is the code using which i am getting data:
<?php
header('Content-Type: text/html; charset=UTF-8');
$request = file_get_contents('php://input');
//$req_dump = print_r( $request, true );
$fp = file_put_contents( 'request.log', urldecode(json_encode($request)));
?>
output :
domain_prefix = test & payload = {
"attributed_cost": "0.00",
"count": 50,
"id": "da674e25-5d9c-a8e2-1d5e-aa018b6cc1c3",
"outlet": {
"id": "3faf0608-91fa-11e3-a0f5-b8ca3a64f8f4",
"name": "West End Shop",
"tax_id": "e52b2846-e920-11e5-f98b-29c175501027",
"time_zone": "Australia/Brisbane"
},
"outlet_id": "3faf0608-91fa-11e3-a0f5-b8ca3a64f8f4",
"product": {
"active": true,
"attributed_cost": null,
"base_name": "Test Productss",
"button_order": null,
"categories": [],
"deleted_at": null,
"description": "",
"handle": "testproducts",
"id": "bfe919c4-6357-46c9-a333-d1db636446c5",
"name": "Test Productss",
"sku": "10203",
"source": "USER",
"source_id": null,
"source_variant_id": null,
"supply_price": "0.00",
"taxes": [{
"outlet_id": "3faf0608-91fa-11e3-a0f5-b8ca3a64f8f4",
"tax_id": "e52b2846-e920-11e5-f98b-29c175501027"
}],
"variant_options": [],
"variant_parent_id": null
},
"product_id": "bfe919c4-6357-46c9-a333-d1db636446c5",
"reorder_point": "0",
"restock_level": "0",
"version": 16073287694
} & retailer_id = 3 fa8309c - 91 fa - 11e3 - a0f5 - b8ca3a64f8f4 & type = inventory.update
I am not sure how to fetch "count" & "id" & "product_id" attributes value from this.
I have tried parse_str but it didn't work.
let me know your thoughts on this, how to fetch data from it ?
Thankyou
if your $request is like that output then you can try
you can try
parse_str($request, $parse_request);
$payload = json_decode($parse_request["payload"], true);
$id = $payload["id"];
$count = $payload["count"];
$product_id = $payload["product"]["id"];
Related
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 }?>
Here is my scenario, for example i have 4000 contacts (return 271257 lines after json_encode) in database, i have called
echo json_encode($response);
But it takes 35-40 second to return response as JSON. So my question is, are there any another options for decrease converting process to json. So i can make fast application.
I have tried a lot by calling below functions
1) json_encode($response, JSON_UNESCAPED_UNICODE);
2)
header('Content-Type: application/json')
echo json_encode($response);
3)
header('Content-Type: application/json')
echo json_encode($response); die;
This is the sample data of single contact
{
"address": {
"primary": {
"id": "",
"address1": "",
"address2": "",
"city": "",
"state": "",
"zipcode": "",
"country": "",
"primary-address": ""
},
"secondary": {
"id": "",
"address1": "",
"address2": "",
"city": "",
"state": "",
"zipcode": "",
"country": "",
"secondary-address": ""
}
},
"groups": [],
"regions": [],
"links": [],
"phone": [
{
"type_id": 2,
"phone_type": "Mobile",
"country_code": "us",
"value": "+1 1830593231",
"is_primary": 1,
"country_code_value": "+1"
}
],
"social": [],
"email": [
{
"type_id": 2,
"type": "Work",
"value": "cron_4000#gmail.com",
"is_primary": 1
}
],
"tags": [],
"action": {
"is_favourite": 0,
"is_following": 0,
"is_stay": 0,
"kit_expired": 0
},
"access": [],
"activity_count": [],
"contact_id": 5768,
"type": 0,
"salutation_id": 1,
"assigned_to": 1,
"first_name": "Cron 4000",
"assigned_name": "Krunal Patel",
"created_date": null,
"primary_address_id": 0,
"secondary_address_id": 0,
"modified_date": null,
"contact_modified_date": "0000-00-00 00:00:00",
"created_by": 1
}
its not json_encode fault.if you run below code you can see that in less than 3 seconds it gonna finished.its because of output size and rendering time that you show to the user.you can paginate your output and don't show all of them together
<?php
$str = [];
for($i = 0;$i<4000;$i++){
$str[] = "sample data";
}
$json = json_encode($str);
echo strlen($json);
I have really have troubles trying to work out why this is not working, and it seems like it should be easy, but just cannot seem to get it.
All I am looking to do is grab the sku field (which is VBP-01 below).
{
"variants": [
{
"id": 2314578,
"created_at": "2014-07-29T07:22:18.921Z",
"updated_at": "2015-05-21T15:42:42.136Z",
"product_id": 1188647,
"default_ledger_account_id": null,
"buy_price": "124.0",
"committed_stock": "0",
"incoming_stock": "3",
"composite": false,
"description": null,
"is_online": false,
"keep_selling": false,
"last_cost_price": "124.0",
"manage_stock": true,
"max_online": null,
"moving_average_cost": "124",
"name": "Lanparte battery pinch VBP-01",
"online_ordering": false,
"opt1": null,
"opt2": null,
"opt3": null,
"position": 1,
"product_name": "Lanparte V-Mount Battery Pinch",
"product_status": "active",
"product_type": null,
"retail_price": "0.0",
"sellable": true,
"sku": "VBP-01",
"status": "active",
"stock_on_hand": "1",
"supplier_code": "VBP-01",
"taxable": true,
"upc": null,
"weight": null,
"wholesale_price": "0.0",
"image_ids": [],
"variant_prices": [
{
"price_list_id": "buy",
"value": "124.0"
},
{
"price_list_id": "retail",
"value": "0.0"
},
{
"price_list_id": "wholesale",
"value": "0.0"
}
],
"locations": [
{
"location_id": 16377,
"stock_on_hand": "1",
"committed": null,
"incoming": "3",
"bin_location": null,
"reorder_point": 3
}
],
"prices": {
"buy": "124.0",
"retail": "0.0",
"wholesale": "0.0"
},
"stock_levels": {
"16377": "1.0"
},
"committed_stock_levels": {},
"incoming_stock_levels": {
"16377": "3.0"
}
}
],
"meta": {
"total": 1
}
}
Currently I using the following code with no luck
$url = "https://api.tradegecko.com/variants?sku=VBP-01";
$data = file_get_contents($url, false, $context);
$json = json_decode($data);
print $json->{'variant'}->{'sku'};
What I am doing wrong?
Just
echo $json->{'variants'}[0]->{'sku'};
In a loop
foreach ($json->variants as $variants) {
echo $variants->sku;
}
Below is json response and I have problems getting shipping_address data.
{
"orders": [{
"created_at": "2015-01-04T02:20:03+01:00",
"financial_status": "paid",
"id": 384536708,
"total_price": "45.00",
"line_items": [{
"fulfillment_service": "manual",
"fulfillment_status": null,
"gift_card": false,
"grams": 0,
"price": "45.00",
"quantity": 1,
"requires_shipping": true,
"taxable": true,
"title": "test",
"vendor": "test",
"name": "test",
"properties": [],
"product_exists": true,
"fulfillable_quantity": 1,
"tax_lines": []
}],
"shipping_address": {
"address1": "Street 20",
"address2": "",
"city": "City",
"company": "",
"country": "USA",
"first_name": "Name",
"last_name": "Surname",
"latitude": 45.000000,
"longitude": 10.000000,
"phone": "",
"province": "",
"zip": "12345",
"name": "Name Surname",
"country_code": "US",
"province_code": null
}
}, [...]
I can get orders with this code:
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json['orders'] as $order){
echo "<td>".$order['id']."</td>";
}
But when I try to get shipping_address data like this:
foreach ($json['shipping_address'] as $sa) {
echo "<td>".$sa['name']."</td>";
}
Then i get: Notice: Undefined index: shipping_address and Warning: Invalid argument supplied for foreach()
Am I targeting array wrong?
It should be -
foreach ($json['orders']['shipping_address'] as $sa) {
Try this -
foreach($json['orders'] as $order){
echo "<td>".$order['id']."</td>";
echo "<td>".$order['shipping_address']['name']."</td>";
}
foreach ($json['orders']['shipping_address'] as $sa) {
echo "<td>".$sa['name']."</td>";
}
Try this :
$content = file_get_contents($url);
$json = json_decode($content, true);
foreach($json as $orders){
foreach($orders as $odarr){
foreach($odarr['shipping_address'] as $shipadd){
echo $shipadd['address1'];
}
}
}
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.