1

我想分享这一点,并寻求其他人的测试来磨练这段代码。如果您使用 maven 链接,这可能有助于您从在线表单在那里制作项目。我们使用表单提交来使用我们的文件服务器等执行几个内部功能。

无论如何,这里是代码。进一步的测试需要专门围绕 PUT 功能完成:

///// CREATE THE MAVEN LINK PROJECT /////


function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
        if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

// MAKE SURE CURL DOES NOT OUTPUT TO PAGE
curl_setopt($curl,  CURLOPT_RETURNTRANSFER, true);

   //SET HEADERS FOR AUTH
   curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer !---INSERT-0-AUTH-TOKEN---!', 'Expect:'
));

//PUT REQUIRES ADDITIONAL HEADERS
if ($method == "PUT") 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT',
'Authorization: Bearer !---INSERT-0-AUTH-TOKEN---!', 'Expect:'
));

curl_setopt($curl, CURLOPT_URL, $url);

$vars = curl_exec($curl);

return $vars;
}
// CREATE THE WORKSPACE: NOTE, I left my post variables in here - however should be noted that a script was used (not included) to ensure against injection.
// build the data according to the jason standards for mavenlink's API
$Data = array('workspace[title]' => $_POST['Description'], 'workspace[creator_role]'=> "maven", 'workspace[description]'=> strip_tags($_POST['Details']), 'workspace[price]'=>$_POST['CostBudget'], 'workspace[due_date]'=>date("Y-m-d",strtotime($_POST['ProjectDue'])), 'workspace[project_tracker_template_id]'=> "285045" );

$DecodeMe = CallAPI("POST","https://api.mavenlink.com/api/v1/workspaces.json",$Data);
$Decoded = json_decode($DecodeMe,true); // be sure to set the option to true so your array comes back as associative


// INVITE USERS:

//////// BASE USERS

$InviteUrl = "https://api.mavenlink.com/api/v1/workspaces/".$Decoded['results'][0]['id']."/invite.json";


$Data2 = array('invitation[full_name]' => "User Fullname", 'invitation[email_address]'=> "email@email.com", 'invitation[invitee_role]'=> "maven" );
$Decoded2 = json_decode(CallAPI("POST",$InviteUrl,$Data2),true);

// 重复上述操作或使用 do while 脚本处理更多。

// THE END
4

0 回答 0