【发布时间】:2016-12-19 15:18:27
【问题描述】:
我是 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 变量为空,而不是返回带有板列表的 json 文件
有人知道如何使这段代码工作吗?
【问题讨论】:
-
如果对您有任何帮助,请查看此链接:blog.clarkrasmussen.com/2013/06/25/php-and-the-trello-api