1. Authentication API
The below code is for PHP 7.3, PHP server side must support TLS 1.2.
1.1. Get Your credentials from developer portal
$client_id = "Your client id";
$client_secret = "Your secret key";
$callback_url = "Your callback URL";
$partner_csn = = "Your csn";
$client_secret = "Your secret key";
$callback_url = "Your callback URL";
$partner_csn = = "Your csn";
1.2. Create Signature
$time_stamp = strtotime("now");
$base_str = $callback_url.$client_id.$time_stamp;
$hmacsha256 = hash_hmac('sha256', $base_str, $client_secret, true);
$signature = base64_encode($hmacsha256);
$base_str = $callback_url.$client_id.$time_stamp;
$hmacsha256 = hash_hmac('sha256', $base_str, $client_secret, true);
$signature = base64_encode($hmacsha256);
1.3. Create Authorization
$base_64_message = $client_id.":".$client_secret;
$base_64_encoded = base64_encode($base_64_message);
$base_64_encoded = base64_encode($base_64_message);
1.4. Call Authentication API to get access token
$curl = curl_init();
curl_setopt_array($curl, array(CURLOPT_URL => "https://enterprise-api-stg.autodesk.com/v2/oauth/generateaccesstoken?grant_type=client_credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => ""
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ".$base_64_encoded,
"cache-control: no-cache",
"signature: ".$signature,
"timestamp: ".$time_stamp
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$decoded_response = json_decode($response, true);
$access_token = $decoded_response['access_token'];
return $access_token;
}
}
$access_token = getToken($client_id,$client_secret,$callback_url);
?>
curl_setopt_array($curl, array(CURLOPT_URL => "https://enterprise-api-stg.autodesk.com/v2/oauth/generateaccesstoken?grant_type=client_credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => ""
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ".$base_64_encoded,
"cache-control: no-cache",
"signature: ".$signature,
"timestamp: ".$time_stamp
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$decoded_response = json_decode($response, true);
$access_token = $decoded_response['access_token'];
return $access_token;
}
}
$access_token = getToken($client_id,$client_secret,$callback_url);
?>
Response:
access_token => ZbsJ0Nj2kwPNHxV7V9KuS1EkaYhS
This access token will be used in next code sample.
2. Get Order Details
2.1 Get your CSN, order number, make sure access token has been created by authentication API.
$client_id = "your client id";
$client_secret = "your secret key";
$callback_url = "your callback URL";
$partner_csn = "partner CSN";
$client_secret = "your secret key";
$callback_url = "your callback URL";
$partner_csn = "partner CSN";
Note - GetOrderDetails API has other parameters as well. Please refer to the API documentation and modify code accordingly.
2.2 Create Signature
$time_stamp = strtotime("now");
$base_str = $callback_url.$access_token.$time_stamp;
$base_str = $callback_url.$access_token.$time_stamp;
$hmacsha256 = hash_hmac('sha256', $base_str, $client_secret, true);
$signature = base64_encode($hmacsha256);
$base_str = $callback_url.$access_token.$time_stamp;
$base_str = $callback_url.$access_token.$time_stamp;
$hmacsha256 = hash_hmac('sha256', $base_str, $client_secret, true);
$signature = base64_encode($hmacsha256);
2.3 Call Get Order API
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://enterprise-api-stg.autodesk.com/v1/orders/?customer_number=".$partner_csn."&contract_number=".$contract_number,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$access_token,
"signature: ".$signature,
"timestamp: ".$time_stamp,
"csn: ".$partner_csn,
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response, true);
$message = $result['message'];
return $message;
}
}
$order_details = getOrderDetails($access_token,$client_secret,$callback_url,$partner_csn,$contract_number);
?>
curl_setopt_array($curl, array(
CURLOPT_URL => "https://enterprise-api-stg.autodesk.com/v1/orders/?customer_number=".$partner_csn."&contract_number=".$contract_number,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$access_token,
"signature: ".$signature,
"timestamp: ".$time_stamp,
"csn: ".$partner_csn,
"cache-control: no-cache",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$result = json_decode($response, true);
$message = $result['message'];
return $message;
}
}
$order_details = getOrderDetails($access_token,$client_secret,$callback_url,$partner_csn,$contract_number);
?>
Response:
message => elements:[{"order_header_array":[{"order_date":"2018-11
22T00:00:00+0000","sales_order_number":"7058108640",......
22T00:00:00+0000","sales_order_number":"7058108640",......
3. Get value from the JSON response
$message = $result['message'];
$elements = $message['elements'];
$arr = $elements['0'];
$order_header = $arr['order_header_array'];
$arr2 = $order_header['0'];
$contract_number = $arr2['contract_number'];
echo "Contract Number: ";
var_dump($contract_number);;
$elements = $message['elements'];
$arr = $elements['0'];
$order_header = $arr['order_header_array'];
$arr2 = $order_header['0'];
$contract_number = $arr2['contract_number'];
echo "Contract Number: ";
var_dump($contract_number);;