0

我正在尝试简单地将待办事项添加到待办事项列表中。我在这里有一个非常基本的功能,我知道它是身份验证,如果我的密码错误,它确实会给我权限被拒绝错误。当我使用所有正确的信息运行它时,它返回一个空字符串,而不是它在 Create Todo 下描述的内容并且不添加任务。

我可以按预期进行 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) );
4

1 回答 1

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>";
}

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

于 2013-07-02T12:57:39.323 回答