【问题标题】:Cannot read data from Patreon JSON API无法从 Patreon JSON API 读取数据
【发布时间】:2019-02-04 22:33:04
【问题描述】:

使用 Packagist (https://packagist.org/packages/patreon/patreon?q=&p=6) 提供的代码,我无法获得预期的结果。我的代码现在让用户登录,并返回他们的数据(我可以通过 var_dump 查看),但我在实际阅读它时遇到了问题。

根据 Patreon API 文档,从 API 接收到的数据会自动设置为数组,除非另​​有说明。我正在从他们的网站运行确切的代码,但他们的 API 正在返回一个对象,我不确定如何从中读取用户的数据和承诺信息。我尝试将返回数据设置为数组或 json,但没有任何运气。当我将 API 响应转换为数组时,我只是得到了这个混乱的混乱。

截图 - https://i.gyazo.com/3d19f9422c971ce6e082486cd01b0b92.png

require_once __DIR__.'/vendor/autoload.php';

use Patreon\API;
use Patreon\OAuth;

$client_id = 'removed';
$client_secret = 'removed';

$redirect_uri = "https://s.com/redirect";

$href = 'https://www.patreon.com/oauth2/authorize?response_type=code&client_id=' . $client_id . '&redirect_uri=' . urlencode($redirect_uri);

$state = array();

$state['final_page'] = 'http://s.com/thanks.php?item=gold';

$state_parameters = '&state=' . urlencode( base64_encode( json_encode( $state ) ) );

$href .= $state_parameters;

$scope_parameters = '&scope=identity%20identity'.urlencode('[email]');

$href .= $scope_parameters;

echo '<a href="'.$href.'">Click here to login via Patreon</a>';

if (isset($_GET['code']))
{
    $oauth_client = new OAuth($client_id, $client_secret);  
    $tokens = $oauth_client->get_tokens($_GET['code'], $redirect_uri);

    $access_token = $tokens['access_token'];
    $refresh_token = $tokens['refresh_token'];

    $api_client = new API($access_token);
    $campaign_response = $api_client->fetch_campaign();

    $patron = $api_client->fetch_user();
    $patron = (array)$patron;
    die(var_dump($patron));
}

我希望能够查看用户的数据和质押信息。我已经尝试过诸如 $patron->data->first_name、$patron['data']['first_name'] 之类的东西,它们都抛出了关于找不到数组索引的错误。

【问题讨论】:

  • 看起来它正在返回一个 JSON 对象 - 你试过 json_decode() 吗?您将结果视为对象,但查看响应时需要将其视为多维关联数组。
  • @Sam 在$api_client-&gt;fetch_user() 上使用json_decode() 返回json_decode() expects parameter 1 to be string, object given in。如果我在使用 json_decode() 之前将其转换为字符串,我会得到:Object of class Art4\JsonApiClient\Document could not be converted to string
  • 刚刚快速浏览了 API 文档,我看不到任何 fetch_user 方法 - 加上任何获取用户数据的调用都需要您为用户传递某种标识符。您确定此调用符合您的预期吗?

标签: php patreon


【解决方案1】:

您可能已经想出了一些办法,但我遇到了同样的事情并想在这里分享一个解决方案。

patreon 库返回的 JSONAPIResource 对象具有一些特定方法,可以以可读格式返回单个数据。

import patreon
from pprint import pprint

access_token = <YOUR TOKEN HERE>   # Replace with your creator access token

api_client = patreon.API(access_token)
campaign_response = api_client.fetch_campaign()

campaign = campaign_response.data()[0]
pprint(campaign.id()) # The campaign ID (or whatever object you are retrieving)
pprint(campaign.type()) # Campaign, in this case
pprint(campaign.attributes()) # This is most of the data you want
pprint(campaign.attribute('patron_count')) # get a specific attribute
pprint(campaign.relationships()) # related entities like 'creator' 'goals' and 'rewards'
pprint(campaign.relationship('goals'))
pprint(campaign.relationship_info()) 
# This last one one ends up returning another JSONAPIResource object with it's own method: .resource() that returns a list of more objects 
# for example, to print the campaign's first goal you could use:
pprint( campaign.relationship_info('goals').resource()[0].attributes() )

希望对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2013-10-04
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多