Site icon EcomPlugins Blog

Netsuite how to get inventory details by suiteql php curl

To get inventory details from NetSuite using SuiteQL via PHP and cURL, you’ll need to set up a REST API request to execute the SuiteQL query. Here’s how you can achieve this:

Prerequisites

  1. NetSuite Account: You need access to a NetSuite account with the necessary permissions to run SuiteQL queries.
  2. Token-Based Authentication (TBA): Ensure you have the necessary credentials (consumer key, consumer secret, token, and token secret) for TBA.

Steps

  1. Set Up the PHP Environment: Make sure you have PHP installed and cURL enabled.
  2. Create the PHP Script: Write a PHP script to execute the SuiteQL query via NetSuite’s REST APIHere is an example PHP script:
    <?php
    
    // NetSuite TBA credentials
    $accountId = 'YOUR_ACCOUNT_ID';
    $consumerKey = 'YOUR_CONSUMER_KEY';
    $consumerSecret = 'YOUR_CONSUMER_SECRET';
    $tokenId = 'YOUR_TOKEN_ID';
    $tokenSecret = 'YOUR_TOKEN_SECRET';
    
    // SuiteQL query
    $suiteql = "
        SELECT
            item.id AS item_id,
            item.itemid AS item_name,
            inventorynumber.inventorynumber AS serial_number,
            inventorynumber.location AS location_id,
            inventorynumber.quantityavailable AS quantity_available
        FROM
            item
        JOIN
            inventorynumber ON item.id = inventorynumber.item
        WHERE
            item.type = 'InvtPart'
    ";
    
    // OAuth 1.0a parameters
    $oauth_nonce = bin2hex(random_bytes(16));
    $oauth_timestamp = time();
    $oauth_signature_method = 'HMAC-SHA256';
    $oauth_version = '1.0';
    
    // Create the base string for the OAuth signature
    $baseString = 'POST&' . urlencode('https://<account_id>.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql') . '&' . urlencode(http_build_query([
        'oauth_consumer_key' => $consumerKey,
        'oauth_nonce' => $oauth_nonce,
        'oauth_signature_method' => $oauth_signature_method,
        'oauth_timestamp' => $oauth_timestamp,
        'oauth_token' => $tokenId,
        'oauth_version' => $oauth_version,
    ]));
    
    // Create the signing key
    $signingKey = urlencode($consumerSecret) . '&' . urlencode($tokenSecret);
    
    // Create the OAuth signature
    $oauth_signature = base64_encode(hash_hmac('sha256', $baseString, $signingKey, true));
    
    // Prepare the Authorization header
    $authHeader = 'OAuth ' . http_build_query([
        'oauth_consumer_key' => $consumerKey,
        'oauth_nonce' => $oauth_nonce,
        'oauth_signature' => urlencode($oauth_signature),
        'oauth_signature_method' => $oauth_signature_method,
        'oauth_timestamp' => $oauth_timestamp,
        'oauth_token' => $tokenId,
        'oauth_version' => $oauth_version,
    ], '', ', ');
    
    // Initialize cURL
    $ch = curl_init();
    
    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, 'https://<account_id>.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['q' => $suiteql]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: ' . $authHeader,
        'Prefer: transient',
    ]);
    
    // Execute the request and get the response
    $response = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        // Parse and display the response
        $result = json_decode($response, true);
        print_r($result);
    }
    
    // Close cURL
    curl_close($ch);
    
    ?>
    

    Explanation:

    • OAuth 1.0a Authentication: NetSuite’s REST API requires OAuth 1.0a for authentication. The script generates the necessary OAuth parameters and signs the request.
    • SuiteQL Query: The SuiteQL query is defined in the $suiteql variable.
    • cURL Request: The script initializes a cURL session, sets the necessary options, and sends the request to NetSuite’s REST API endpoint for SuiteQL.
    • Response Handling: The response from NetSuite is decoded from JSON and printed.

    Important:

    • Replace placeholders like YOUR_ACCOUNT_ID, YOUR_CONSUMER_KEY, etc., with your actual NetSuite credentials.
    • Adjust the SuiteQL query as needed for your specific requirements.
    • Ensure that your NetSuite account has the appropriate permissions to access the data and run SuiteQL queries.

    This script should give you a good starting point for fetching inventory details from NetSuite using SuiteQL via PHP and cURL.

Exit mobile version