How to connect Netsuite Rest using php and postman collection and run suiteQL

Md Mazaharul Huq
3 min readApr 27, 2024

Connecting to NetSuite’s REST Web Services might sound like a daunting task, but fear not! With the right information, it’s as easy as pie. Here’s a step-by-step guide to help you get started:

Step 1: Get Your Secret Codes

Before you can dive into the world of NetSuite, you’ll need some secret codes to access the magic. These include:

  • Consumer Secret: This is like your secret password to access NetSuite’s services securely.
  • Access Token: Think of this as your special ticket that grants you entry into NetSuite’s world.
  • Token Secret: Another secret code that adds an extra layer of security to your connection.
  • Company ID: This identifies your company within NetSuite. It’s crucial to get this right for a smooth connection.

Step 2: Watch the NetSuite REST Web Services Demo

To learn how to obtain these values, check out the NetSuite REST Web Services Demo video on YouTube. It’ll guide you through the process in an easy-to-follow manner.

Step 3: Remember the Company ID Format

Here’s a common mistake many people make: If your sandbox company address looks like “22111_sb1,” make sure to pass the company ID as “22111-SB1.” It’s a small detail but can save you a lot of headaches!

Step 4: Connect to NetSuite

Once you have all the required values, connecting to NetSuite is a breeze. Simply use the provided codes and company ID in your connection script, and voila! You’re ready to explore the wonders of NetSuite’s REST Web Services.

<?php
$company_id = "22111-SB1";
$oauth_consumer_key="0111d619f02cd7288ac31ab0199a95c2fxccxvcccdcc0000xx0";
$oauth_consumer_secret="0111d619f02cd7288ac31ab0199a95c2fxccxvcccdcc0000xx0";


$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://$company_id.suitetalk.api.netsuite.com/services/rest/record/v1/customer",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: OAuth realm="3751499_SB2",oauth_consumer_key="048fd619f02cd7288ac31ab0199a95c2f18d4d06d29dbe225414c27d3bc11a90",oauth_token="a0cb2116e1ce3a6caa0b00157800644c1ecad892500efed0b87dfaad756e0c94",oauth_signature_method="HMAC-SHA256",oauth_timestamp="1713972114",oauth_nonce="d5d7057b-e21a-4c82-88c9-cadf00f41dd9",oauth_signature="6rhiTE6GOj41HAwZxM1HDOBxPXoTyWr1e5q6W5jK8Y8%3D"'
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Now let me explain how to generate the oauth_signature

    function oauth_get_sbs($http_method, $url, $params) {
// Percent encode each key and value in the parameters
$encoded_params = array();
foreach ($params as $key => $value) {
$encoded_params[rawurlencode($key)] = rawurlencode($value);
}

// Sort parameters by key
ksort($encoded_params);

// Join parameters into a string of key-value pairs separated by '&'
$param_string = array();
foreach ($encoded_params as $key => $value) {
$param_string[] = $key . '=' . $value;
}
$param_string = implode('&', $param_string);

// Percent encode the URL and remove query parameters
$url_parts = parse_url($url);
$base_url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
$encoded_base_url = rawurlencode($base_url);

// Concatenate HTTP method, encoded base URL, and encoded parameter string with '&'
$sbs = strtoupper($http_method) . '&' . $encoded_base_url . '&' . rawurlencode($param_string);

return $sbs;
}
public function testSignature()
{
$url = 'https://123456.suitetalk.api.netsuite.com/services/rest/record/v1/employee/40';
$httpMethod = 'GET'; //or $httpMethod = 'GET'; for REST Web Services
$tokenKey = '2b0ce516420110bcbd36b69e99196d1b7f6de3c6234c5afb799b73d87569f5cc';
$tokenSecret = 'c29a677df7d5439a458c063654187e3d678d73aca8e3c9d8bea1478a3eb0d295';
$consumerKey = 'ef40afdd8abaac111b13825dd5e5e2ddddb44f86d5a0dd6dcf38c20aae6b67e4';
$consumerSecret = 'd26ad321a4b2f23b0741c8d38392ce01c3e23e109df6c96eac6d099e9ab9e8b5';
$signatureMethod = 'HMAC-SHA256';
$nonce = 'fjaLirsIcCGVZWzBX0pg'; //substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20);
$timestamp = '1508242306'; //time();
$version = '1.0';
$realm = '123456'; //scompid

$baseString = $this->oauth_get_sbs($httpMethod, $url, array('oauth_consumer_key' => $consumerKey,
'oauth_nonce' => $nonce,
'oauth_signature_method' => $signatureMethod,
'oauth_timestamp' => $timestamp,
'oauth_token' => $tokenKey,
'oauth_version' => $version));

$key = rawurlencode($consumerSecret) .'&'. rawurlencode($tokenSecret);
$signature = base64_encode(hash_hmac('sha256', $baseString, $key, true));
echo rawurlencode($signature);

}

//Reference : https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_1534941088.html#Web-Services-Signature

//Reference : https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_1534941088.html#Web-Services-Signature

In case if you want to connect netsuite using native php follow this

https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_N3534011.html

--

--