使用Maven Link V1 API和PHP

时间:2013-10-16 11:43:41

标签: php json api curl

我想分享这个,并寻求其他人的测试来磨练这段代码。如果您使用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

0 个答案:

没有答案