I am using curl function to get the data from gotowebinar url. here is the code
$data=curl_exec($curl);
#curl_close($curl);
$newdata=json_decode($data, true);
print_r($newdata);
I am getting this output:
[
{
"registrantKey": 12345,
"firstName": "xxx",
"lastName": "xxx",
"email": "xxx#yahoo.com",
"status": "WAITING",
"registrationDate": "2012-07-11T16:54:11Z",
"joinUrl": "https://www1.gotomeeting.com/join/123/456",
"timeZone": "America/New_York"
},
{
"registrantKey": 12345,
"firstName": "xxx",
"lastName": "xxx",
"email": "xxx#yahoo.com",
"status": "WAITING",
"registrationDate": "2012-07-05T23:55:23Z",
"joinUrl": "https://www1.gotomeeting.com/join/123/456",
"timeZone": "America/New_York"
},
{
"registrantKey": 12345,
"firstName": "xxx",
"lastName": "xxx",
"email": "xxx#yahoo.com",
"status": "WAITING",
"registrationDate": "2012-07-11T23:27:56Z",
"joinUrl": "https://www1.gotomeeting.com/join/123/456",
"timeZone": "America/Chicago"
},
{
"registrantKey": 12345,
"firstName": "xxx",
"lastName": "xxx",
"email": "xxx#visioninvesting.com",
"status": "WAITING",
"registrationDate": "2012-07-11T23:29:40Z",
"joinUrl": "https://www1.gotomeeting.com/join/123/456",
"timeZone": "America/Chicago"
},
{
"registrantKey": 12345,
"firstName": "xxx",
"lastName": "xxx",
"email": "xxx#yahoo.com",
"status": "WAITING",
"registrationDate": "2012-07-11T18:14:32Z",
"joinUrl": "https://www1.gotomeeting.com/join/123/456",
"timeZone": "America/Chicago"
},
{
"registrantKey": 12345,
"firstName": "test",
"lastName": "1",
"email": "xxx#yahoo.com",
"status": "WAITING",
"registrationDate": "2012-06-29T21:07:10Z",
"joinUrl": "https://www1.gotomeeting.com/join/123/456",
"timeZone": "America/Denver"
}
]
I used json_decode to format the data but it did not work. I want to format the output so that I can use its values in program.
Here is some simple PHP code for looping over the resulting object from calling json_decode.
$newdata = json_decode($data);
foreach($newdata as $entry) {
echo "{$entry->firstName} is {$entry->status}. " .
"Their key is {$entry->registrantKey}.<br />\n";
}
You can access any of the properties you see in the returned json from the decoded object.
Since you get an array of objects, you can loop over each entry (as shown above), or access a specific entry like this:
$third = $newdata[2]->firstName;
Hope that helps get you started.
Try converting it to an array first:
$newdata = (array) json_decode($data, true);
Related
I need to somehow handle the following recursive log output using PHP, creating an array or encoding it in json, the ultimate goal is to retrieve the "id" value of each getusers section, i've done some tests but i can't get a good result.
This is just a portion of the log string containing all the getusers sections(there can be thousands):
getusers: {
"user": {
"id": 1569976517,
"type": "user",
"first_name": "Mike",
"username": "MikesNick",
"verified": false,
"restricted": false,
"status": {
"_": "userStatusRecently"
},
"access_hash": -7018287513210933137,
"bot_nochats": false
},
"date": 1613450429,
"role": "user"
}
getusers: {
"user": {
"id": 717418136,
"type": "user",
"first_name": "John",
"last_name": "Smith",
"username": "JhonsNick",
"verified": false,
"restricted": false,
"status": {
"_": "userStatusOffline",
"was_online": 1613230815
},
"access_hash": 1814429223003924316,
"bot_nochats": false
},
"date": 1613245788,
"role": "user"
}
What is the best way to manage it and recover the data I need?
I found a way to extract the IDs from the log using the following code
$exploded = explode("getusers:",$logString);
foreach($exploded as $group){
$decoded = json_decode($group,true);
echo $decoded["user"]["id"]."<br/>";
}
I have the following json :-
{
"firstName": "Jhon",
"lastName": "Doe",
"username": "jhon",
"avatar": "localhost/uploads/avatars/default.jpg",
"language": "ar",
"birth_date": "2017-11-22 00:00:00",
"weight_chart": [],
"health_status": {
"id": 130,
"user_id": 258,
"weight": 95,
"height": 171,
},
I decoded the above json
$user = json_decode($response);
Now i am able to print the firstname by using:
$user->firstName
My questions is :-
Can i access the json values without decoding it ?
How can i access the "health_status" values id, weight ... ?
Your json is malformed, try this structure:
$str = '{
"firstName": "Jhon",
"lastName": "Doe",
"username": "jhon",
"avatar": "localhost/uploads/avatars/default.jpg",
"language": "ar",
"birth_date": "2017-11-22 00:00:00",
"weight_chart": [],
"health_status": {
"id": 130,
"user_id": 258,
"weight": 95,
"height": 171
}
}';
$obj = json_decode($str);
echo $obj->firstName.' - ';
echo $obj->health_status->id.' - ';
echo $obj->health_status->weight;
response: Jhon - 130 - 95
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
{
"result": "success",
"clientid": "1",
"serviceid": null,
"pid": null,
"domain": null,
"totalresults": "2",
"startnumber": 0,
"numreturned": 2,
"products": {
"product": [
{
"id": "1",
"clientid": "1",
"orderid": "1",
"pid": "1",
"regdate": "2015-01-01",
"name": "Starter",
"translated_name": "Starter",
"groupname": "Shared Hosting",
"translated_groupname": "Shared Hosting",
"domain": "demodomain.com",
"dedicatedip": "",
"serverid": "1",
"servername": "Saturn",
"serverip": "1.2.3.4",
"serverhostname": "saturn.example.com",
"suspensionreason": "",
"firstpaymentamount": "12.95",
"recurringamount": "12.95",
"paymentmethod": "authorize",
"paymentmethodname": "Credit Card",
"billingcycle": "Monthly",
"nextduedate": "2016-11-25",
"status": "Terminated",
"username": "demodoma",
"password": "xxxxxxxx",
"subscriptionid": "",
"promoid": "0",
"overideautosuspend": "",
"overidesuspenduntil": "0000-00-00",
"ns1": "",
"ns2": "",
"assignedips": "",
"notes": "",
"diskusage": "0",
"disklimit": "0",
"bwusage": "0",
"bwlimit": "0",
"lastupdate": "0000-00-00 00:00:00",
"customfields": {
"customfield": []
},
"configoptions": {
"configoption": []
}
},
{
"id": "2",
"clientid": "1",
"orderid": "2",
"pid": "3",
"regdate": "2015-05-20",
"name": "Plus",
"translated_name": "Plus",
"groupname": "Shared Hosting",
"translated_groupname": "Shared Hosting",
"domain": "demodomain2.net",
"dedicatedip": "",
"serverid": "2",
"servername": "Pluto",
"serverip": "2.3.4.5",
"serverhostname": "pluto.example.com",
"suspensionreason": "",
"firstpaymentamount": "24.95",
"recurringamount": "24.95",
"paymentmethod": "paypal",
"paymentmethodname": "PayPal",
"billingcycle": "Monthly",
"nextduedate": "2017-01-20",
"status": "Active",
"username": "demodom2",
"password": "xxxxxxxx",
"subscriptionid": "",
"promoid": "0",
"overideautosuspend": "",
"overidesuspenduntil": "0000-00-00",
"ns1": "",
"ns2": "",
"assignedips": "",
"notes": "",
"diskusage": "0",
"disklimit": "0",
"bwusage": "0",
"bwlimit": "0",
"lastupdate": "0000-00-00 00:00:00",
"customfields": {
"customfield": []
},
"configoptions": {
"configoption": [
{
"id": "1",
"option": "Sample Config Option",
"type": "dropdown",
"value": "Selected option value"
}
]
}
}
]
}
}
How can i parse the product details from json
You can use json decode function of php to make object then fetch data from object.
$data = 'write your json response here';
$return = json_decode($data);
print_r($return);
echo $return->result;
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 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.