Search code examples
phpapicurltrello

Inviting people with Trello API


i'm trying to invite a person via email to Trello from my website. Here is the API reference. When I try to invite him, the plain reply is "invalid key". Here is my function:

public function inviteEmployeeToTrello ($email, $name, $isAdmin)
{
    $organazationTrelloID = 'myOrganazationID';
    $trelloAuthToken = 'myTrelloAuthToken';
    $trelloInviteUrl = 'https://trello.com/1/organizations/'.$organazationTrelloID.'/members';

    if ($isAdmin == 1)
    {
        $type = 'admin';
    }
    else
    {
        $type = 'normal';
    }

    $fields = array(
        'fullName' => $name,
        'email' => $email,
        'type' => $type,
        'token' => $trelloAuthToken
    );

    // open connection
    $ch = curl_init();

    // set the url, number of PUT vars, PUT data
    curl_setopt($ch, CURLOPT_URL, $trelloInviteUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // exec
    $replyRaw = curl_exec($ch);
    $reply = json_decode($replyRaw, true);

    // close connection
    curl_close($ch);

    dd($ch);
}

Solution

  • CURLOPT_POSTFIELDS does not want a JSON, if you want a urlencoded request, use http_build_query($fields) , or if you want a multipart/form-data request, just give it $fields array directly. (the API doc's doesn't seem to mention which request types it accept, though. urlencoded is the most common one.)