Basecamp API using cURL and PHP

家住魔仙堡 提交于 2019-12-23 05:49:10

问题


I'm trying to connect to the basecamp api (json) using PHP + cURL, but all my attempts have failed, and I'm not sure why. The end goal is to build a dashboard that displays information from basecamp projects to team members over http.

<?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);
?>

回答1:


I am working on a very similar project as you are right now. You have to create a Basecamp app first. Than the following code should get you connected:

$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);


来源:https://stackoverflow.com/questions/15329476/basecamp-api-using-curl-and-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!