php - 使用 trello API PHP 获取信息

标签 php trello

我是 php 初学者,我想使用服务器上的 php 脚本(此处为 localhost --> wamp)从我的 trello 帐户获取一些信息

我使用了在互联网上找到的简单 PHP 代码向 trello api 发出一些请求。

它包含在 trello-api 类 (trello-api.php)

<?php
  class trello_api {
    private $key;
    private $secret;
    private $token;

    public function __construct ($key, $secret, $token) {
      $this->key = $key;
      $this->secret = $secret;
      $this->token = $token;
    }

    public function request ($type, $request, $args = false) {
      if (!$args) {
        $args = array();
      } elseif (!is_array($args)) {
        $args = array($args);
      }

      if (strstr($request, '?')) {
        $url = 'https://api.trello.com' . $request . '&key=' . $this->key . '&token=' . $this->token;
      } else {
        $url = 'https://api.trello.com' . $request . '?key=' . $this->key . '&token=' . $this->token;
      }

      $c = curl_init();
      curl_setopt($c, CURLOPT_HEADER, 0);
      curl_setopt($c, CURLOPT_VERBOSE, 0);
      curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($c, CURLOPT_URL, $url);

      if (count($args)) curl_setopt($c, CURLOPT_POSTFIELDS , http_build_query($args));

      switch ($type) {
        case 'POST':
          curl_setopt($c, CURLOPT_POST, 1);
          break;
        case 'GET':
          curl_setopt($c, CURLOPT_HTTPGET, 1);
          break;
        default:
          curl_setopt($c, CURLOPT_CUSTOMREQUEST, $type);
      }

      $data = curl_exec($c);
      curl_close($c);

      return json_decode($data);
    }
  }
?>

我将文件放在wamp目录中../www/trello/trello-api.php

我创建了另一个文件index.php

<?php
require "./trello_api.php";
$key = 'my_key';
$secret = 'my_secret';
$token = 'my_token';
$trello = new trello_api($key, $secret, $token);

$data = $trello->request('GET', ('1/boards/'));

echo $data;

?>

$data 变量为 null,而不是返回带有板列表的 json 文件

有人知道如何使这段代码工作吗?

最佳答案

$secret 未在您的 trello_api 类中使用,可以删除。 API URL 格式不正确,因为域和路径之间没有斜杠。此外,我还向 CURL 添加了 CURLOPT_CAINFO 选项,因为 API 使用 HTTPS 协议(protocol)。

我的 trello-api.php:

<?php

class trello_api
{
    private $key;
    private $token;

    public function __construct($key, $token)
    {
        $this->key = $key;
        $this->token = $token;
    }

    public function request($type, $request, $args = false)
    {
        if (!$args) {
            $args = array();
        } elseif (!is_array($args)) {
            $args = array($args);
        }

        if (strstr($request, '?')) {
            $url = 'https://api.trello.com/1/' . $request . '&key=' . $this->key . '&token=' . $this->token;
        } else {
            $url = 'https://api.trello.com/1/' . $request . '?key=' . $this->key . '&token=' . $this->token;
        }

        $c = curl_init();
        curl_setopt($c, CURLOPT_HEADER, 0);
        curl_setopt($c, CURLOPT_VERBOSE, 0);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_CAINFO, dirname(__FILE__) .  '/trello.com.crt');

        if (count($args)) curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($args));

        switch ($type) {
            case 'POST':
                curl_setopt($c, CURLOPT_POST, 1);
                break;
            case 'GET':
                curl_setopt($c, CURLOPT_HTTPGET, 1);
                break;
            default:
                curl_setopt($c, CURLOPT_CUSTOMREQUEST, $type);
        }

        $data = curl_exec($c);
        echo curl_error($c);
        curl_close($c);

        return json_decode($data);
    }
}

/trello.com.crt 是从 Trello API URL 上的 FireFox 导出的证书文件。

我的index.php:

<?php
require "./trello-api.php";
$key = 'key';
$token = 'token';
$trello = new trello_api($key, $token);

$data = $trello->request('GET', ('member/me/boards'));

echo '<pre>';
print_r($data);
echo '</pre>';

API 响应示例:

Array
(
    [0] => stdClass Object
        (
            [name] => TESt
            [desc] => 
            [descData] => 
            [closed] => 
            [idOrganization] => 
            [pinned] => 
            [invitations] => 
            [shortLink] => sqzLZoin
            [powerUps] => Array
                (
                )

            [dateLastActivity] => 
            [idTags] => Array
                (
                )

            [id] => 57adee08e0ba2754693cc3de
            [invited] => 
            [starred] => 
            [url] => https://trello.com/b/sqzLZoin/test
            [prefs] => stdClass Object
                (
                    [permissionLevel] => private
                    [voting] => disabled
                    [comments] => members
                    [invitations] => members
                    [selfJoin] => 
                    [cardCovers] => 1
                    [cardAging] => regular
                    [calendarFeedEnabled] => 
                    [background] => blue
                    [backgroundImage] => 
                    [backgroundImageScaled] => 
                    [backgroundTile] => 
                    [backgroundBrightness] => dark
                    [backgroundColor] => #0079BF
                    [canBePublic] => 1
                    [canBeOrg] => 1
                    [canBePrivate] => 1
                    [canInvite] => 1
                )

            [memberships] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 57adee08e0ba2754693cc3df
                            [idMember] => 57adedff402470436a70280b
                            [memberType] => admin
                            [unconfirmed] => 
                            [deactivated] => 
                        )

                )

            [subscribed] => 
            [labelNames] => stdClass Object
                (
                    [green] => 
                    [yellow] => 
                    [orange] => 
                    [red] => 
                    [purple] => 
                    [blue] => 
                    [sky] => 
                    [lime] => 
                    [pink] => 
                    [black] => 
                )

            [dateLastView] => 2016-08-12T15:41:11.371Z
            [shortUrl] => https://trello.com/b/sqzLZoin
        )

    [1] => stdClass Object
        (
            [name] => Добро пожаловать на доску
            [desc] => 
            [descData] => 
            [closed] => 
            [idOrganization] => 
            [pinned] => 
            [invitations] => 
            [shortLink] => huMmEapC
            [powerUps] => Array
                (
                )

            [dateLastActivity] => 2016-08-12T15:40:47.911Z
            [idTags] => Array
                (
                )

            [id] => 57adedff402470436a702811
            [invited] => 
            [starred] => 
            [url] => https://trello.com/b/huMmEapC/-
            [prefs] => stdClass Object
                (
                    [permissionLevel] => private
                    [voting] => disabled
                    [comments] => members
                    [invitations] => members
                    [selfJoin] => 1
                    [cardCovers] => 1
                    [calendarFeedEnabled] => 
                    [background] => blue
                    [backgroundImage] => 
                    [backgroundImageScaled] => 
                    [backgroundTile] => 
                    [backgroundBrightness] => dark
                    [backgroundColor] => #0079BF
                    [canBePublic] => 1
                    [canBeOrg] => 1
                    [canBePrivate] => 1
                    [canInvite] => 1
                )

            [memberships] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 57adedff402470436a702815
                            [idMember] => 4e6a7fad05d98b02ba00845c
                            [memberType] => normal
                            [unconfirmed] => 
                            [deactivated] => 
                        )

                    [1] => stdClass Object
                        (
                            [id] => 57adedff402470436a702816
                            [idMember] => 57adedff402470436a70280b
                            [memberType] => admin
                            [unconfirmed] => 
                            [deactivated] => 
                        )

                )

            [subscribed] => 
            [labelNames] => stdClass Object
                (
                    [green] => 
                    [yellow] => 
                    [orange] => 
                    [red] => 
                    [purple] => 
                    [blue] => 
                    [sky] => 
                    [lime] => 
                    [pink] => 
                    [black] => 
                )

            [shortUrl] => https://trello.com/b/huMmEapC
        )

)

关于php - 使用 trello API PHP 获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38920843/

相关文章:

php - Laravel 覆盖 EloquentUserProvider 以更改 validateCredentials() 中的密码字段名称

javascript - 表单发布不接受链接

php - PDO 数组到字符串的转换 - 参数

Trello API : determine when a card changed lists?

api - 特雷洛 API : Getting invalid token on all calls

javascript - 如何在php和数据库中的opentracker()中传递动态id

php - 如何在 dompdf 库上启用调试?

python - 如何通过 Trello API 发布和检索评论卡数据?

security - 验证 Trello Webhook 签名

trello - 向 Trello Card 添加自定义属性