【问题标题】:GoCardless API - List SubscriptionsGoCardless API - 列出订阅
【发布时间】:2020-12-10 01:25:08
【问题描述】:

我正在使用 GoCardless 文档here 尝试列出客户的所有订阅。

我已按照您在下面看到的说明进行操作,但是当我运行此脚本时根本没有显示任何内容 - 有人知道我可能做错了什么吗?

require 'vendor/autoload.php';    
$client = new \GoCardlessPro\Client(array(
  'access_token' => 'XXXXXx',
  'environment'  => \GoCardlessPro\Environment::LIVE
));

     $client->subscriptions()->list([
  "params" => ["customer" => "CU000R3B8512345"]
]);

【问题讨论】:

  • 使用邮递员之类的东西来验证您的请求是否应该返回数据
  • @Kevin 抱歉,什么是“邮递员”?
  • 你只是在执行一个命令。您没有返回或打印任何内容,因此不会显示任何内容。
  • @MartinBean - 我该怎么做马丁?我尝试将整行转换为变量并打印它,但这也失败了$result=$client->subscriptions()->list([...
  • 是的,它不会显示任何仅调用该方法的内容。你需要对结果做点什么。

标签: php api gocardless


【解决方案1】:

单独调用一个方法不会做任何事情。它会执行给定的方法,但不会自行在浏览器屏幕上打印任何内容。

正如RiggsFolly 所说(并记录在 GoCardless 的 API 文档中),调用 $client->subscriptions()->list()返回 cursor-paginated response 对象。所以你需要对这个结果做点什么。那是什么,我不知道,因为这是您应用程序的业务逻辑,只有您知道。

<?php

use GoCardlessPro\Client;
use GoCardlessPro\Environment;

require '../vendor/autoload.php';

$client = new Client(array(
    'access_token' => 'your-access-token-here',
    'environment' => Environment::SANDBOX,
));

// Assign results to a $results variable
$results = $client->subscriptions()->list([
    'params' => ['customer' => 'CU000R3B8512345'],
]);

foreach ($results->records as $record) {
    // $record is a variable holding an individual subscription record
}

【讨论】:

  • @Designer 除了 Martin 的注释之外,你在 foreach 循环中添加了什么吗?
  • 显示Catchable fatal error: Object of class GoCardlessPro\Core\ListResponse could not be converted to string i
  • @Designer - echo"&lt;pre&gt;$record&lt;/pre&gt;"; 你会看到结果的!
  • 试试print_r($record);,它将显示对象的外观,以便您可以使用实际属性
  • @Designer 现在已经说过几次调用该方法将返回一个对象。 它不会在屏幕上打印任何内容必须对返回的对象做点什么!
【解决方案2】:

使用 Gocardless 进行分页:

function AllCustomers($client)
{
    $list = $client->customers()->list(['params'=>['limit'=>100]]);
    $after = $list->after;
    // DO THINGS
    print_r($customers);

    while ($after!="")
    {
        $customers = $list->records;
        // DO THINGS
        print_r($customers);
        
        // NEXT
        $list = $client->customers()->list(['params'=>['after'=>$after,'limit'=>100]]);
        $after = $list->after;
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 2021-04-24
    • 2020-11-28
    • 2014-04-24
    • 2022-01-01
    • 2013-12-02
    • 2011-12-05
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多