从条带到 PHP 变量的 Json 响应


Json response from stripe to variables in php

我在php中有一个代码,可以侦听条纹,它会向我的服务器发送json格式的信息。我想从 json 中检索电子邮件并将其分配给变量$email。我不明白我的php代码有什么问题。

PHP代码:

<?php  
include "connect.php"; 
echo "Works"; 
require_once('stripe_final/init.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
'Stripe'Stripe::setApiKey("this is hidden key");
$input = @file_get_contents("php://input");
$event_json = json_decode($input); 
$event_id = $event_json->id;
if(isset($event_json->id)) {
    try {
        // to verify this is a real event, we re-retrieve the event from Stripe 
        //  $event = Stripe_Event::retrieve($event_id);
        // successful payment
        if($event->type == 'charge.succeeded') {
            // send a payment receipt email here
            // retrieve the payer's information                     
            $email = $event_json->name; 
            $sql = "INSERT INTO stripe (email) VALUES ('$email')";
            if (mysqli_query($db, $sql)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($db);
            }
            mysqli_close($db);
        } 
    } catch (Exception $e) {
    }       
}
http_response_code(200); // PHP 5.4 or greater
?> 

Json 代码:

    {
  "id": "evt_7nxAKynL74HXUu",
  "object": "event",
  "api_version": "2012-11-07",
  "created": 1454008591,
  "data": {
    "object": {
      "id": "ch_7nxA7QWn3069zJ",
      "object": "charge",
      "amount": 15000,
      "amount_refunded": 0,
      "application_fee": null,
      "balance_transaction": "txn_7nxAqCOclHRnAv",
      "captured": true,
      "card": {
        "id": "card_7nxAJFJ52PhS8G",
        "object": "card",
        "address_city": null,
        "address_country": null,
        "address_line1": null,
        "address_line1_check": null,
        "address_line2": null,
        "address_state": null,
        "address_zip": null,
        "address_zip_check": null,
        "brand": "Visa",
        "country": "US",
        "customer": "cus_7nxAxI4lGICtyv",
        "cvc_check": "pass",
        "dynamic_last4": null,
        "exp_month": 11,
        "exp_year": 2022,
        "fingerprint": "lEO6YJCu2ASyQbvB",
        "funding": "credit",
        "last4": "4242",
        "metadata": {},
        "name": "futjakot@gmail.com",
        "tokenization_method": null,
        "type": "Visa"
      },
      "created": 1454008591,
      "currency": "gbp",
      "customer": "cus_7nxAxI4lGICtyv",
      "description": null,
      "destination": null,
      "dispute": null,
      "failure_code": null,
      "failure_message": null,
      "fraud_details": {},
      "invoice": null,
      "livemode": false,
      "metadata": {},
      "order": null,
      "paid": true,
      "receipt_email": "futjakot@gmail.com",
      "receipt_number": null,
      "refunded": false,
      "refunds": [],
      "shipping": null,
      "source": {
        "id": "card_7nxAJFJ52PhS8G",
        "object": "card",
        "address_city": null,
        "address_country": null,
        "address_line1": null,
        "address_line1_check": null,
        "address_line2": null,
        "address_state": null,
        "address_zip": null,
        "address_zip_check": null,
        "brand": "Visa",
        "country": "US",
        "customer": "cus_7nxAxI4lGICtyv",
        "cvc_check": "pass",
        "dynamic_last4": null,
        "exp_month": 11,
        "exp_year": 2022,
        "fingerprint": "lEO6YJCu2ASyQbvB",
        "funding": "credit",
        "last4": "4242",
        "metadata": {},
        "name": "futjakot@gmail.com",
        "tokenization_method": null,
        "type": "Visa"
      },
      "statement_descriptor": null,
      "status": "paid",
      "statement_description": null,
      "fee": 455,
      "fee_details": [
        {
          "amount": 455,
          "amount_refunded": 0,
          "application": null,
          "currency": "gbp",
          "description": "Stripe processing fees",
          "type": "stripe_fee"
        }
      ]
    }
  },
  "livemode": false,
  "pending_webhooks": 1,
  "request": "req_7nxAFmUej9UkFy",
  "type": "charge.succeeded"
}

实际上,该 JSON 中有 3 封电子邮件。

$event_json = json_decode($input);
$email1 = $event_json->data->object->card->name;
$email2 = $event_json->data->object->receipt_email;
$email3 = $event_json->data->object->source->name;

您可以通过对$event_json进行一系列var_dump陈述来自己弄清楚。

如您所见,名称在对象命名数据中对象命名的对象卡中,因此电子邮件的检索应为:

$email = $event_json->data->object->card->name;