Я новичок в php, и я хочу получить некоторую информацию из моей учетной записи trello, используя скрипт php на сервере (здесь 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 со списком досок
Кто-нибудь знает, как заставить этот код работать?
$ secret не используется в вашем классе trello_api и может быть удален. URL API был сформирован неправильно, потому что между доменом и путем не было косой черты. Также я добавил опцию 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 по URL Trello API.
Мой 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
)
)
Других решений пока нет …