2016-08-01 57 views
0

我想邀請一個人通過電子郵件從我的網站Trello。這裏是API reference。當我嘗試邀請他時,簡單回覆是「無效密鑰」。這裏是我的功能:邀請Trello API的人

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

回答

1

CURLOPT_POSTFIELDS不想要一個JSON, 如果你想要一個urlencoded請求,使用http_build_query($ fields),或者如果你想要一個multipart/form-data請求,直接給它$ fields數組。 (API文檔似乎沒有提及它接受哪種請求類型,urlencoded是最常見的。)

+0

謝謝!我已將我的密鑰添加到$ field數組,並用http_build_query($ fields)替換了json_encode($ fields),現在它可以工作了! – coolside

1

由於錯誤代碼說,你忘了通過你的application key。下面是來自API reference一個例子:

https://api.trello.com/1/organizations/publicorg?members=all&member_fields=username,fullName&fields=name,desc&鍵= [application_key] &令牌= [optional_auth_token]

你必須把它列入您的查詢,因此你的情況,將其添加到在 $fields陣列:

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

謝謝!我已將我的密鑰添加到$ field數組,並用http_build_query($ fields)替換了json_encode($ fields),現在它可以工作了! – coolside