7 Replies Latest reply on May 25, 2016 2:59 PM by jmsanch

    Orion API via PHP

    aront

      One of the things I have been tasked with doing is understanding the API to see if we can use it for some of our reporting. I reviewed the Orion API documentation and there aren’t any PHP examples. I also can not find any such examples in thwack. Can anyone help me with an example? I have spent some time trying to figure this out and I can’t tell if I am doing something wrong or if SWIS is even available.

        • Re: Orion API via PHP
          tdanner

          I recommend using the JSON/REST API for SWIS. There are a number of REST clients available for PHP. If you are familiar with one, try using that. If not, "Guzzle" came up as the first hit on Google and appears to be a wrapper for cURL, which is quite powerful and works fine with the SWIS REST API.

           

          Let us know how it goes!

            • Re: Orion API via PHP
              blodwick

              It would be really nice to get a working example of PHP interacting with the JSON/REST API for SWIS. I am trying to go super simple to start with. Do you guys have any ideas what I may be doing wrong?

               

              <?

              function pullit($url,$data) {

               

               

              $ch = curl_init();

              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

              curl_setopt($ch, CURLOPT_HEADER, 0);

              curl_setopt($ch, CURLOPT_POST, true);

              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

              curl_setopt($ch, CURLOPT_USERAGENT, "Chrome 35.0.1916.153");

              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

              curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");

              curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");

              curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

              curl_setopt($ch, CURLOPT_URL, $url);

              $response = curl_exec($ch);

               

               

              echo "<pre>RESPONSE<br>";

              print_r($response);

              echo "</pre><br>";

               

               

              curl_close ($ch);

              }

               

               

              $url = 'https://10.10.1.1:17778/SolarWinds/InformationService/v3/Json/Query';

               

               

              $username = 'domain\devops';

              $password = 'password';

               

               

              $jsonData = array(

                  'Content-Type' => 'application/json',

                'Username' => $username,

                  'Password' => $password,

                'query' => 'SELECT Comments FROM Orion.Nodes WHERE NodeID=1'

              );

               

               

              $postdata = json_encode($jsonData);

              pullit($url,$postdata);

              ?>

                • Re: Re: Orion API via PHP
                  tdanner

                  User jfoley posted some PHP code for running a query in this thread: Re: Orion SDK Information

                   

                  ini_set('display_errors', 'On');

                    error_reporting(E_ALL);

                   

                    $endpoint = "https://localhost:17778/SolarWinds/InformationService/v3/Json/Query";

                   

                    $post_data = array(

                    'query' => "SELECT TOP 1 DisplayName, DetailsUrl FROM Orion.Nodes"

                    );

                   

                    $strdata = json_encode($post_data);

                   

                    $ch = curl_init($endpoint);

                   

                    curl_setopt($ch, CURLOPT_POST, 1);

                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

                    curl_setopt($ch, CURLOPT_POSTFIELDS, $strdata);

                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                    curl_setopt($ch, CURLOPT_USERPWD, "admin:");

                    //curl_setopt($ch, CURLOPT_HEADER, 1); // This will output response headers

                    curl_setopt($ch, CURLOPT_HTTPHEADER, array(

                      'Content-Type: application/json',

                      'Content-Length: ' . strlen($strdata)

                    ));

                   

                    $result = curl_exec($ch);

                    exit("Result: {$result}");

                  • Re: Re: Orion API via PHP
                    jfoley

                    Try adding: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

                    • Re: Orion API via PHP
                      aront

                      It took me awhile to get something working, but here is what I was able to use:

                       

                      <?php

                      $data = array(

                        'query' => "SELECT TOP 1 DisplayName, DetailsUrl FROM Orion.Nodes"

                        );

                      $url = "https://oriondev:17778/SolarWinds/InformationService/v3/Json/Query";

                      $jdata = json_encode($data);

                      echo $result = CallAPI($url, $jdata);

                       

                       

                      function CallAPI($url, $data = false) {

                         $ch = curl_init();

                       

                          // Authentication:

                          curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

                          curl_setopt($ch, CURLOPT_USERPWD, "username:password");

                          curl_setopt($ch, CURLOPT_URL, $url);

                          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                        curl_setopt($ch, CURLOPT_SSLVERSION, 3);

                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

                        curl_setopt($ch, CURLOPT_VERBOSE, true);

                        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(

                                                              'Content-type: application/json',

                                                              'Content-length: ' . strlen($data)

                                                            ));

                       

                        $result = curl_exec($ch);

                        $response = curl_getinfo($ch, CURLINFO_HTTP_CODE);

                        $error = curl_error($ch);

                        $info = curl_getinfo($ch);

                       

                        curl_close($ch);

                       

                          if ($result == false) {

                        echo "Response: " . $response . "<br>";

                        echo "Error: " . $error . "<br>";

                        echo "Info: " . print_r($info);

                        die();

                        }

                       

                        return $result;

                      }

                      ?>

                       

                  • Re: Orion API via PHP
                    jmsanch

                    I know this is old, but I wanted to expand a little on what the OP had answered.  Your answer worked perfectly for me.  I want to take it a step further and use the results from the json call, parse it, and use the data to create a custom board.  I've played around with php for awhile, but this is my first time messing with json.  I've googled ways to do this, but I just can't get it to work right.  Anyone try this before maybe help me a bit? To Expand on this I'm at least trying to get it into an array, and see if I can echo values I can use for conditional statements etc..

                     

                    <?php include 'includes/functions.php';

                     

                     

                    $data = array('query' => "SELECT Sysname,StatusLED  FROM Orion.Nodes WHERE StatusLED = 'Down.gif'");

                     

                     

                    $url = "https://10.0.51.140:17778/SolarWinds/InformationService/v3/Json/Query";

                    $jdata = json_encode($data);

                     

                     

                    //echo $result = CallAPI($url, $jdata);

                     

                     

                    $result = json_decode(CallAPI($url, $jdata));

                     

                     

                    $name = $result['Sysname'];

                     

                     

                    echo $name;