“同等看待欢乐和痛苦、得到和失去、胜利和失败、投入战斗。以此方式履行职责,你就不会招致任何罪恶。”
-Bhagavad Gita
为了帮助PHP开发者快速起步,以REST Web Services方式打通与NetSuite的接口,我们答应给一个样例。但是我是不懂PHP的,在经历了ChatGPT误导和帮助,调通了代码。心生欢喜,分享如下。
<?php
class NetSuiteRestAPI {
// NetSuite Credentials
const NETSUITE_ACCOUNT = '大写账号';
const NETSUITE_CONSUMER_KEY = '你的参数';
const NETSUITE_CONSUMER_SECRET = '你的参数';
const NETSUITE_TOKEN_ID = '你的参数';
const NETSUITE_TOKEN_SECRET = '你的参数';
// Search a Employee record
public function searchEmployee($employeeId) {
$url = "https://小写域名.suitetalk.api.netsuite.com/services/rest/record/v1/employee/$employeeId";
return $this->callNetSuiteAPI($url, "GET", $employeeId);
}
// Create a new Employee record
public function createEmployee($employeeData) {
$url = "https://小写域名.suitetalk.api.netsuite.com/services/rest/record/v1/employee";
return $this->callNetSuiteAPI($url, "POST", $employeeData);
}
// Call NetSuite API
private function callNetSuiteAPI($url, $method, $payload = null) {
$oauth_nonce = md5(mt_rand());
$oauth_timestamp = time();
$oauth_signature_method = 'HMAC-SHA256';
$oauth_version = "1.0";
// Generate Signature
$baseString = $this->generateBaseString($method, $url, self::NETSUITE_CONSUMER_KEY, self::NETSUITE_TOKEN_ID, $oauth_nonce, $oauth_timestamp, $oauth_version, $oauth_signature_method, $payload);
$key = rawurlencode(self::NETSUITE_CONSUMER_SECRET) . '&' . rawurlencode(self::NETSUITE_TOKEN_SECRET);
$signature = base64_encode(hash_hmac('sha256', $baseString, $key, true));
// Generate OAuth Header
$headers = [
'Authorization: OAuth '
. 'realm="' . self::NETSUITE_ACCOUNT . '", '
. 'oauth_consumer_key="' . self::NETSUITE_CONSUMER_KEY . '", '
. 'oauth_token="' . self::NETSUITE_TOKEN_ID . '", '
. 'oauth_nonce="' . $oauth_nonce . '", '
. 'oauth_timestamp="' . $oauth_timestamp . '", '
. 'oauth_signature_method="' . $oauth_signature_method . '", '
. 'oauth_version="' . $oauth_version . '", '
. 'oauth_signature="' . rawurlencode($signature) . '"',
'Content-Type: application/json'
];
// Call the API
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
//CURLOPT_VERBOSE =>true,
]);
if ($payload) {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
}
$response = curl_exec($curl);
if ($response === false) {
echo 'cURL Error: ' . curl_error($curl);
return false;
}
curl_close($curl);
return json_decode($response, true);
}
// Generate Base String
private function generateBaseString($httpMethod, $url, $consumerKey, $tokenKey, $nonce, $timestamp, $version, $signatureMethod, $postParams = null) {
$baseString = strtoupper($httpMethod) . '&' . rawurlencode($url) . '&';
// Prepare OAuth parameters
$params = [
'oauth_consumer_key' => $consumerKey,
'oauth_token' => $tokenKey,
'oauth_nonce' => $nonce,
'oauth_timestamp' => $timestamp,
'oauth_signature_method' => $signatureMethod,
'oauth_version' => $version,
];
// Sort parameters by key
ksort($params);
// Encode and concatenate parameters
$encodedParams = [];
foreach ($params as $key => $value) {
$encodedParams[] = rawurlencode($key) . '=' . rawurlencode($value);
}
$baseString .= rawurlencode(implode('&', $encodedParams));
return $baseString;
}
}
// Instantiate the API class
$api = new NetSuiteRestAPI();
// Call to search a Employee record
$employeeData=员工InternalID;
$response = $api->searchEmployee($employeeData);
// Output the response
if ($response) {
echo "Response: " . json_encode($response, JSON_PRETTY_PRINT);
}
/*
// Employee data to create
$employeeData = [
"firstname" => "R",
"lastname" => "M"
];
// Call to create a new Employee record
$response = $api->createEmployee($employeeData);
// Output the response
if ($response) {
echo "Response: " . json_encode($response, JSON_PRETTY_PRINT);
} else {
echo "Call Successfully.";
}
*/
?>
如果有任何关于NetSuite的问题,欢迎来谈。邮箱:service@truston.group