如何从Stripe JSON对象获取二级数据

时间:2015-07-17 22:28:16

标签: php json stripe-payments

我有能够检索1级数组数据的函数,但是在收费后返回JSON字符串中没有级别2。

else if(!empty($_SESSION['SESS_PLAN_BUY_CODE'])){
       $pcharge = \Stripe\Customer::create(array(
       "source" => $_POST['stripeToken'],
       "plan" => $_SESSION['SESS_PLAN_BUY_CODE'],
       "email" => $_SESSION["SESS_EMAIL"]
       ));
    echo "My customer id is: " . $pcharge->id;
    echo'<br>';
    $stuff = json_decode($pcharge->subscriptions);
    var_dump($stuff);

实际JSON返回字符串: JSON

上面代码的输出是:

我的客户id是:cus_6cvCQdpdhmQlGe NULL

预期产出:

我的客户id是:cus_6cvCQdpdhmQlGe 1

我试过了:

$stuff = $pcharge->subscriptions[0];
$stuff = $pcharge->subscriptions['total_count'];
$stuff = json_decode($pcharge->subscriptions);

并且很少循环 我得到NULL或其他一些错误,表明程序员不知道他在做什么。

如何获取Stripe JSON返回字符串中的level 2level 3数据?

1 个答案:

答案 0 :(得分:0)

您无需致电json_decode(),Stripe API会在内部进行所有解码。您只需要访问它返回的对象和数组。

$pcharge = \Stripe\Customer::create(array(
    "source" => $_POST['stripeToken'],
    "plan" => $_SESSION['SESS_PLAN_BUY_CODE'],
    "email" => $_SESSION["SESS_EMAIL"]
));
echo "My customer id is: " . $pcharge->id;
echo'<br>';
$stuff = $pcharge->subscriptions;
echo "Subscription count is: " . count($stuff->data); // total_count isn't returned by default

如果您想访问订阅,请使用$stuff->data[$i]