PHPとJSONは初めてなので、誰かが助けてくれることを願っています。
サードパーティが POST を実行し、JSON データを配信する PHP があります。送信されるデータの例は次のとおりです。
> { "created": 1326853478, "livemode": false, "id":
> "evt_00000000000000", "type": "charge.succeeded", "object":
> "event", "data": {
> "object": {
> "id": "ch_00000000000000",
> "object": "charge",
> "created": 1366838716,
> "livemode": false,
> "paid": true,
> "amount": 1000,
> "currency": "gbp",
> "refunded": false,
> "fee": 59,
> "fee_details": [
> {
> "amount": 59,
> "currency": "usd",
> "type": "stripe_fee",
> "description": "Stripe processing fees",
> "application": null,
> "amount_refunded": 0
> }
> ],
> "card": {
> "object": "card",
> "last4": "4242",
> "type": "Visa",
> "exp_month": 2,
> "exp_year": 2016,
> "fingerprint": "cniJDyeew54ashW6Iyr",
> "country": "US",
> "name": "wibble3",
> "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
> },
> "captured": true,
> "failure_message": null,
> "amount_refunded": 0,
> "customer": null,
> "invoice": null,
> "description": null,
> "dispute": null
> } } }
これらの値に応じて、特定のアイテムを抽出して処理できるようにしたいと考えています。
このコードを使用すると、「タイプ」を簡単に抽出できます。
$body = @file_get_contents('php://input');
$event_json = json_decode($body);
print $event_json->{'type'};
しかし、たとえば、値を正常に抽出しているレベルより下のレベルにあると思われる「料金」や、別のレベルにある「説明」を抽出することはできません。
この JSON 文字列から特定の項目 (たとえば、Type、Fee、Description、Last4) のみを抽出できるようにしたいのですが、完全に迷っています。これはかなり基本的な作業である可能性が高いと思いますが、どこにも行きません。
どんな助けでも、ありがたく受け取った!