2015-06-30 91 views
0

我想訪問我的JSON數組中的值。這是我的PHP代碼來獲取JSON內容:PHP - 訪問JSON數組

$input = @file_get_contents("php://input"); 
$event_json = json_decode($input); 
$event = \Stripe\Event::retrieve($event_id); 

這是輸出:

Stripe\Event JSON: { 
"id": "evt_6WV8LzHBPwRYW4", 
"created": 1435683755, 
"livemode": false, 
"type": "charge.succeeded", 
"data": { 
"object": { 
"id": "ch_6WV8a3Sdj3ihE2", 
"object": "charge", 
"created": 1435683755, 
"livemode": false, 
"paid": true, 
"status": "succeeded", 
"amount": 5000, 
"currency": "usd", 
"refunded": false, 
"source": { 
"id": "card_6WV8kV4c36X1Gl", 
"object": "card", 
"last4": "4242", 
"brand": "Visa", 
"funding": "credit", 
"exp_month": 2, 
"exp_year": 2022, 
"fingerprint": "QXn7Az3ZTTaLBbhx", 
"country": "US", 
"name": "[email protected]", 
"address_line1": null, 
"address_line2": null, 
"address_city": null, 
"address_state": null, 
"address_zip": null, 
"address_country": null, 
"cvc_check": "pass", 
"address_line1_check": null, 
"address_zip_check": null, 
"tokenization_method": null, 
"dynamic_last4": null, 
"metadata": [], 
"customer": "cus_6WV87uyazq5L64" 
}, 
"captured": true, 
"balance_transaction": "txn_6WV8Bz6aZSB3tb", 
"failure_message": null, 
"failure_code": null, 
"amount_refunded": 0, 
"customer": "cus_6WV87uyazq5L64", 
"invoice": null, 
"description": null, 
"dispute": null, 
"metadata": { 
"product": "deposit" 
}, 
"statement_descriptor": null, 
"fraud_details": [], 
"receipt_email": null, 
"receipt_number": null, 
"shipping": null, 
"destination": null, 
"application_fee": null, 
"refunds": { 
"object": "list", 
"total_count": 0, 
"has_more": false, 
"url": "\/v1\/charges\/ch_6WV8a3Sdj3ihE2\/refunds", 
"data": [] 
} 
} 
}, 
"object": "event", 
"pending_webhooks": 1, 
"request": "iar_6WV8y99x1gReuF", 
"api_version": "2015-06-15" 
} 

具體而言,我希望訪問data->metadata->product值 - 在本例中是「定金」

這是我正在試圖訪問它:

$event->data->metadata->product; 

雖然這回報空白。

我在做什麼錯?

+0

有一個對象的屬性下的數據:'$事件 - >數據 - >對象 - > metadata-> product'。在'$ event'上執行'print_r()'來查看結構。 – AbraCadaver

+0

@AbraCadaver不知道我怎麼會錯過這個。這是解決方案。你介意把它作爲答案嗎? – oliverbj

回答

1

data下有object財產。請在$event上執行print_r()查看結構。

用途:$event->data->object->metadata->product

可以在print_r()看到:

stdClass Object 
(
    [id] => evt_6WV8LzHBPwRYW4 
    ..... 
    [data] => stdClass Object 
     (
      [object] => stdClass Object 
       (
        ..... 
        [metadata] => stdClass Object 
         (
          [product] => deposit 
         )