2016-01-28 34 views
0

我有一個代碼在PHP中偵聽條紋,它發送到我的服務器的JSON格式信息。我想從json中檢索電子郵件並將其分配給變量$ email。我不明白我的php代碼有什麼問題。 JSON響應從條帶到變量在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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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

其實你呢在這個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報表已經想通了自己。

1

正如你看到的名字命名對象物體的物體卡在這樣的電子郵件檢索命名對象數據應該是:

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