Basecamp API POST调用不起作用

时间:2012-11-02 20:10:39

标签: php api rest curl basecamp

我正在尝试简单地将todo添加到todolist中。我在这里有一个非常基本的功能,我知道它正在进行身份验证,如果我输错了密码,它确实会给我许可拒绝错误。当我使用所有正确的信息运行它时,它返回一个空字符串,而不是它在Create Todo下描述的here并且不添加任务。

我能够按照我的预期进行GET操作,但POST似乎没有任何影响

这有什么问题,或者其他方式可以获得有关工作原理的更详细信息吗?

    $username = [REDACTED];
    $password = [REDACTED];
    $userid = [REDACTED];

    $projectid = [REDACTED];
    $todolistid = [REDACTED];

    $todo = "make basecamp integration work!";
    $due = date('Y-m-d' , strtotime('+3 weeks'));

    $url = "https://basecamp.com/".$userid."/api/v1/projects/".$projectid."/todolists/".$todolistid."/todos.json";

    $data = '{
      "content": "'.$todo.'",
      "due_at": "'.$due.'",
      "assignee": {
        "id": '.$userid.',
        "type": "Person"
      }
    }';

    $cheaders = array(
        'User-Agent: '.$username,
        'Content-Type: application/json; charset=utf-8'
        );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER,$cheaders);

    if ($data){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    var_dump( curl_exec($curl) );

1 个答案:

答案 0 :(得分:0)

我目前正在开展一个类似的项目,这是我现在所拥有的,也许它会帮助你:

检索项目数据

$appName = 'AppName';
$appContact = 'MyAddress';

$basecampAccountId = 'MyAccountID';
$basecampUsername = 'MyUsername';
$basecampPassword = 'MyPassword';
$baseUrl = "https://basecamp.com/XXXXXXXXXX/api/v1";

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

/*echo $url.'<br>';                                     //Echoes the url of the imported projects
/*echo $credentials.'<br>';*/                           //Echoes the credentials of the person signed in
/*echo $helloHeader.'<br>';*/                           //Echoes the email address of the person signed in

$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);     //optional
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);             //optional
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);     //optional
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);        //optional
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);       //optional
    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);

检索待办事项列表项

$todo = $response;
$todophp = json_decode($todo);

echo "$response";
echo "<br/><br/>";
print_r(array_values($todophp));
echo "<br/><br/>";

foreach($todophp as $tododata)
{   
    echo "<ul>";
    echo "<li>".$tododata->name."</li>";
    echo "<li>".$tododata->id."</li>";
    echo "<li>".$tododata->description."</li>";
    /*echo "<li>".$tododata->;*/

    $url= $baseUrl.'/projects/'.$tododata->id.'/todolists.json';
$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);     //optional
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);             //optional
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);     //optional
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);        //optional
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);       //optional
    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);

    $todo = $response;
    $todophp = json_decode($todo);

    $todo_remain = $todophp[0]->remaining_count;
    $todo_completed = $todophp[0]->completed_count;
    $todo_total = $todo_remain + $todo_completed;

    echo "<BR>";
    echo "<BR>";
    echo "Remaining To-Do Items:";
    echo "<BR>";
    echo $todo_remain;
    echo "<BR>";
    echo "Completed To-Do Items:";
    echo "<BR>";
    echo $todo_completed;
    echo "<BR>";
    echo "Total To-Do Items:";
    echo "<BR>";
    echo $todo_total;
    echo "<BR>";
    echo "Remaining percentage is: <BR>";
    echo round(($todo_remain/$todo_total)*100);
    echo "%";
    echo "<BR>";
    echo "Completed percentage is: <BR>";
    echo round(($todo_completed/$todo_total)*100);
    echo "%";


    echo "</ul>";
}

让我知道;)祝你好运玩得开心(不要忘记改变凭证等)。