使用trello API PHP获取信息

时间:2016-08-12 14:50:50

标签: php trello

我是php的初学者,我希望在服务器上使用php脚本从我的trello帐户获取一些信息(这里是localhost - > wamp)

我使用了一个在互联网上找到的简单的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,而不是返回带有board列表的json文件

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

1 个答案:

答案 0 :(得分:2)

你的trello_api类中没有使用

$ secret,可以将其删除。 API URL未正确形成,因为域和路径之间没有斜杠。此外,我已将CURLOPT_CAINFO选项添加到CURL,因为API正在使用HTTPS协议。

我的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
        )

)