如何使用php从条带响应中检索数据

时间:2015-07-02 12:49:34

标签: php multidimensional-array stripe-payments stripe-connect

这是我的条码交易的PHP代码,用于使用我的平台从客户那里收取申请费用:

$token = $_POST['stripeToken'];

// Create the charge on Stripe's servers - this will charge the user's card
$charge = \Stripe\Charge::create(
  array(
    "amount" => 1000, // amount in cents
    "currency" => "usd",
    "source" => $token,
    "description" => "Event charge",
    "application_fee" => 123 // amount in cents
  ),
  array("stripe_account" => $sInfo->stripe_user_id)
);
echo '<pre>';
print_r($charge);

这是我的回复(部分)

Stripe\Charge Object
(
[_opts:protected] => Stripe\Util\RequestOptions Object
    (
        [headers] => Array
            (
                [Stripe-Account] => acct_16JkaUHzfYmjyH68
            )

        [apiKey] => sk_test_mHnhDuaVjnKmdkEApnYAKfGY
    )

[_values:protected] => Array
    (
        [id] => ch_16K6q5HzfYmjyH786HG5a2gp
        [object] => charge
        [created] => 1435840249
        [livemode] => 
        [paid] => 1
        [status] => succeeded
        [amount] => 1000
        [currency] => usd
        [refunded] => 
        [source] => Stripe\Card Object

我很难从_values中获取值“id =&gt; ch_16K6q5HzfYmjyH786HG5a2gp”:protected array

我尝试了以下语法

$charge->_values:protected and $charge['_values:protected']

但是无法获取响应,可以在这里使用php帮助捕获条带连接事务中的响应

3 个答案:

答案 0 :(得分:8)

对于任何可能遇到此问题的人,Stripe的PHP库都有一个创建数组的功能

public function jsonSerialize()
{
    return $this->__toArray(true);
}

使用它从Object获取可行的数组。 实施例

$charge->jsonSerialize();

答案 1 :(得分:6)

尝试此操作而不是转储整个对象

print $charge->id;

答案 2 :(得分:2)

尝试将对象转换为简单数组形式

$charge->__toArray(TRUE);