Search code examples
phpcurlsassas-stored-process

How can I call a SAS Stored Process from CURL within PHP?


I want to build a web application that calls a SAS stored process and prints the results. I want the authentication to be handled behind the scenes.

The web application is built in PHP and I'll be using CURL to make the request.

Is this possible? What CURL options are necessary?


Solution

  • First ensure your stored process web server is configured correctly by following the instructions located here.

    Create a .php file containing the below code.

    <?php
    
      $parameters = array('_program'  => '/Products/SAS Intelligence Platform/Samples/Sample: Hello World',  // PATH TO STORED PROCESS
                          '_username' => 'mysasusername',
                          '_password' => '{SAS002}EFC0A34D034F489E2E0E03E840D324D6D30964A3', // ENCODED PASSWORD FROM PROC PWENCODE
                          'myParam1'  => 'abc',
                          'myParam2'  => 123
                         );
    
      //
      // CREATE A NEW CURL INSTANCE AND CONFIGURE IT
      //  
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, "http://sas.myserver.com/SASStoredProcess/do?");
      curl_setopt($ch, CURLOPT_PORT, 7980);  // PORT USED TO MAKE STORED PROCESS REQUESTS
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // DISABLE SSL CERTIFICATE CHECKING - OPTIONAL DEPENDING ON SERVER CERTIFICATE
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // DISABLE SSL CERTIFICATE CHECKING - OPTIONAL DEPENDING ON SERVER CERTIFICATE
    
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // STORED PROCESS LOGIN INVOLVES MULTIPLE PAGE REQUESTS
      curl_setopt($ch, CURLOPT_COOKIEFILE, ""); // STORED PROCESS LOGIN REQUIRES COOKIES 
      curl_setopt($ch, CURLOPT_COOKIEJAR, ""); // STORED PROCESS LOGIN REQUIRES COOKIES
      curl_setopt($ch, CURLOPT_HEADER, true);  // DONT SUPPRESS HTTP HEADER INFO 
    
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // SUPPRESS DIRECTLY PRINTING RESULTS WHEN CURL_EXEC IS RUN.
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,90); // TIMEOUT LIMIT WHILE TRYING TO CONNECT
      curl_setopt($ch, CURLOPT_TIMEOUT, 90); // TIMEOUT WHILE WAITING FOR RESPONSE
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters)); // SET THIS OPTION LAST.  MUST USE HTTP_BUILD_QUERY CALL ELSE YOU WILL BE PRESENTED WITH LOGIN PAGE
    
      //
      // EXECUTE IS AND SAVE THE RESULTS THEN CLOSE THE CURL OBJECT
      //    
      $response = curl_exec($ch) ; 
    
      // 
      // PARSE OUT THE HTTP HEADER VS THE BODY
      //
      $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
      $header = substr($response, 0, $header_size);
      $body = substr($response, $header_size);  
    
      print $body;
    
      curl_close($ch);   
    
    ?>
    

    Configure the following:

    • _username
    • _password
    • _program (for this example I've used a sample that comes with SAS)
    • CURLOPT_URL
    • CURLOPT_PORT
    • depending on your site architecture you may require additional CURL options but the above should suffice for most cases

    Once you have configured it, enter the URL of the .php file into your browser's address bar. You should see the output:

    Hello World!
    

    The PHP code listed used the minimum # of options required to work. It assumes that at some point you may also want to parse the header data to determine the CONTENT-TYPE of the result.

    Also, when implementing the above code, be sure that any user credentials are stored securely. It is never a good idea to hardcode user credentials into source code (even if the SAS password has been run through PWENCODE).