0

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'.

user982124
  • 4,416
  • 16
  • 65
  • 140

2 Answers2

0
  1. You need to use json_decode() first as I can't see this in your question.

  2. id is under order parent so you need to do sonething like $orderJSON->order->id

  3. 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;
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
0

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().

Amin NAIRI
  • 2,292
  • 21
  • 20