0

我正在尝试使用 PHP + cURL 连接到 basecamp api (json),但我所有的尝试都失败了,我不知道为什么。最终目标是构建一个仪表板,通过 http 将来自 basecamp 项目的信息显示给团队成员。

<?php 
$basecamp_url = 'https://basecamp.com/xxxxxx/api/v1';
$username = 'username';
$password = 'pass';

$session = curl_init();
curl_setopt($session, CURLOPT_URL, $basecamp_url.'/projects.xml');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
$response = curl_exec($session);
curl_close($session);

echo '<pre>';
print_r($response);
?>
4

1 回答 1

0

我正在从事一个与您现在非常相似的项目。您必须先创建一个 Basecamp 应用程序。比以下代码应该让您建立联系:

$appName = 'your app name';
$appContact = 'your app email;

$basecampAccountId = 'xxxxx';
$basecampUsername = 'youremailhere';
$basecampPassword = 'yourpassword here';
$baseUrl = "https://basecamp.com/$basecampAccountId/api/v1";

$url= $baseUrl.'/projects.json';
$credentials = "$basecampUsername:$basecampPassword";
$helloHeader = "User-Agent: $appName ($appContact)";

echo $url.'<br>';
echo $credentials.'<br>';
echo $helloHeader.'<br>';

$ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, $credentials);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($helloHeader));

    $response = curl_exec($ch);
    $errno = curl_errno($ch);
    $error = curl_error($ch);
    curl_close($ch);

    print_r($response);
于 2013-03-13T20:25:02.213 回答