在PHP中使用Maven Link V1 API
我想与大家分享这一点,并寻求其他人的测试来磨练这段代码。如果您使用maven链接,这可能有助于您从在线表单在那里创建项目。我们使用表单提交来完成文件服务器等的几个内部功能 无论如何,这是代码。需要专门围绕PUT功能进行进一步的测试:在PHP中使用Maven Link V1 API,php,json,api,curl,Php,Json,Api,Curl,我想与大家分享这一点,并寻求其他人的测试来磨练这段代码。如果您使用maven链接,这可能有助于您从在线表单在那里创建项目。我们使用表单提交来完成文件服务器等的几个内部功能 无论如何,这是代码。需要专门围绕PUT功能进行进一步的测试: ///// CREATE THE MAVEN LINK PROJECT ///// function CallAPI($method, $url, $data = false) { $curl = curl_init(); switch ($m
///// 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