0

I'm trying to simply add a todo to a todolist. I have a pretty basic function here, which I know is authenticating, and it does give me permission denied errors if I have my wrong password. When I run it with all the correct info, it returns an empty string, rather than what it describes here under Create Todo and doesn't add the task.

I'm able to do GET operations as I would expect, but POST doesn't seem to have any effect

Is there something wrong with this, or another way to get more detailed info about what's working?

    $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) );
Damon
  • 10,493
  • 16
  • 86
  • 144

1 Answers1

0

I am currently working on a similar project this is what I have up until now maybe it'll help you out:

Retrieving Project Data

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

Retrieving To-Do Lists items

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

Let me know ;) Good luck have fun (don't forget to alter the credentials etc.)

J.I.N Kleiss
  • 187
  • 1
  • 2
  • 12