2016-12-19 7 views
0

私のサーバーにWebhookからJSON POSTデータが届きました。次のように私は、JSONデータを取得することができます。このJSONを返すPHP JSON値を取得する問題

$orderJSON = file_get_contents('php://input'); 

{ 
    "order": { 
"billing_address": { 
    "address_1": "76 Pacific Drive", 
    "address_2": "", 
    "city": "Bondi", 
    "company": "Hanson Media", 
    "country": "AU", 
    "email": "[email protected]", 
    "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": "[email protected]", 
    "first_name": "Hansel", 
    "last_name": "Gretten", 
    "phone": "212 554 7855", 
    "postcode": "2026", 
    "state": "NSW" 
    }, 
    "email": "[email protected]", 
    "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" 
    } 
} 

私は今、例えば、JSONから個々の要素を取得する必要があります私はid値(3304)を取得したいと思います。

$orderID = $orderJSON->id; 

$orderID = $orderJSON[id]; 

をが、これは単に「非オブジェクトのプロパティを取得しようとすると」のようなエラーを生成します。私が試してみました。

答えて

0

json_decode()を使用して、空ではありません、デコードチェックしなければならない

  • $orderJSON->order->idようsonething行う必要があるので、 order親の下でJSONファイルを翻訳する関数PHP読み込み可能なデータです。

    <?php 
    $json = json_decode($orderJSON); 
    

    あなたのJSONファイルをデコード得れば、あなたの件のデータの素敵な表現を印刷する機能print_r()を使用することができますが。

    <?php 
    print_r($orderJSON); 
    

    これは、ファイルがどのように形成されているかを識別し、希望する値を表示するのに必要なすべての寸法を決定するのに役立ちます。今

    JSON to nice

    あなたが密接に見れば、あなたはあなたのIDを印刷するために、あなたは注文次元を通過する必要があることがわかります。

    希望のIDをターゲットにするには、構文$json->order->idを使用します。

    <?php 
    echo $json->order->id; 
    

    Display the ID from JSON

    PHP : JSON

    PHP : print_r()

    0
    1. 私はあなたの質問でこれが見えないので、最初にjson_decode()を使用する必要があります。

    2. idあなたはまた$orderJSON

    $orderJSON = json_decode($orderJSON); 
    if (empty($orderJSON)) { 
        throw new RuntimeException('Malformed json'); 
    } 
    $orderID = $orderJSON->order->id; 
    
    +0

    引数が不正な場合、 'json_decode'は' null'を返します。 '$ orderJSON'が空でないかどうかをチェックすることが賢明です。それ以外の場合は、 '非オブジェクトのプロパティを取得しようとしています'と同じです。 –

    +0

    @AlexBlexありがとうございます。私は自分の答えを更新しました – Hassaan

    +0

    申し訳ありませんが、それは別の方法です。実際には変数名の再利用がベストプラクティスではないという良い例です。あなたの答えを編集してもらえませんか? –

    関連する問題