Json从条带响应到php中的变量

时间:2016-01-28 19:27:07

标签: php json

我在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"
}

2 个答案:

答案 0 :(得分:1)

实际上,你在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;

您可以通过var_dump上的一系列$event_json语句自行解决。

答案 1 :(得分:0)

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

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