2016-08-12 86 views
1

我在PHP初學者,我想用一臺服務器上的PHP腳本從我trello帳戶的一些信息(這裏本地主機 - > WAMP)獲取信息使用trello API PHP

我用一個簡單的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變量是空的,而不是與主板列表

返回JSON文件3210

有人知道如何讓這段代碼有效嗎?

+0

檢查,如果它是你任何幫助此鏈接:http://blog.clarkrasmussen.com/2013/06/25/php-and-the-trello-api/ –

回答

1

$ secret在你的trello_api類中沒有被使用,並且可以被刪除。由於域和路徑之間沒有斜線,因此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是從Firefox上Trello API URL導出的證書文件。

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

) 
+0

謝謝!只有一個問題,您如何從trello url中導出/trello.com.crt證書文件?因爲我有以下錯誤: 「錯誤設置證書驗證位置:CAfile:C:\ wamp64 \ www \ trello_new/trello.com.crt CApath:none」 –

+1

我已打開https://api.trello.com/1 /在FF中,點擊URL左側的綠色鎖定標誌,然後打開詳細信息,點擊查看認證,詳細信息選項卡,導出爲證書鏈crt/pem。 –

+0

非常感謝!它終於起作用了!你是老闆 –