Friday, November 23, 2018

Invoke an HTTP GET method using Php with RESTlet

 

This is a guide on how to use an HTTP GET method using PHP with RESTlet to retrieve a customer record. The request data is included as one of the parameters of the file_get_contents function and the "application/xml" is not a supported content type.

RESTlet Code

 

function getRecord(request){

      return nlapiLoadRecord(request.recordtype, request.id);

}

 

 

Sample PHP code

 

 

//Host is the external URL found in the RESTlet deployment page

$host ="https://rest.netsuite.com/app/site/hosting/restlet.nl?script=165&deploy=1";

$context = makeContext();

 

$response = file_get_contents($host . "&recordtype=customer&id=163", false, $context);

 

if (!$response)

      echo "Error: cannot read file response."; 

else       

      echo $response;

 

/** Returns a stream context with the Authorization Header set */

function makeContext(){   

      $options = array(

            'http'=>array(

                  'header'=> makeHeaderString(),

                  'method'=>"GET",

                  'timeout'=>300

            )

      );

    return stream_context_create($options);

}

 

 

function makeHeaderString(){

      $account    = "TSTDRV123456";

      $email      = "email@netsuite.com";

      $pass       = "mypassword";

      $role_id    = "3";     

      $content_type = "application/json"; //other possible value: "text/plain"

     

      return      "Authorization: NLAuth nlauth_account=". $account . ", " .

                        "nlauth_email=" . $email . ", " .

                        "nlauth_signature=" . $pass . ", " .

                        "nlauth_role=" . $role_id . "\r\n".

                  "Host: rest.netsuite.com \r\n" .

                  "Content-Type:" . $content_type;   

 

}

 

 

No comments:

Post a Comment