2 Replies Latest reply on Feb 25, 2016 9:18 AM by emguser

    Web Help Desk API, cURL PHP example? (PUT Data)

    emguser

      Question:

      Does anyone have an example of a PHP cURL PUT method for Web Help Desk? !

       

      Trying to accomplish:

      I am trying to link a chromebook(asset) with a student(client) using the PUT method.

      I would also like to link a ticket to an asset.

       

      Other thoughts:

      How do I "PUT"? Do I create and array with my modifications, then to call an "array to JSON" function? If so how do I post the data to this kind of URL? "https://hd.northcantonschools.org/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets/ticket#/?username='. $username . '&password='. $password . '&limit=1000"


      Relevant links:

      From solarwinds: Web Help Desk REST API Guide

       

      Using these videos I was able to create the code below:

      PHP cURL Tutorial Part 1: Basic Structure - YouTube

      PHP cURL Tutorial Part 2: Download Files Using cURL (Cont'd) - YouTube

      PHP cURL Tutorial Part 3: Posting Data To The Server - YouTube

      PHP cURL Tutorial #4: Post Files To Server Using CURLFile Class - YouTube

       

      Tried:

      GET and it works

      <?php
      //PHP cURL GET list of my tickets in JSON
      
      //includes----------------
      //NCCS emg login
      require('include/hd_login.php'); //$username and $password
      //includes----------------
      
      //Variables---------------
      $ch = ""; //curl session id/handler
      $curl_data = ""; //curl return text
      //Variables---------------
      
      //initilize session
      $ch = curl_init();
      
      //set soptions
      curl_setopt($ch,CURLOPT_URL, 'https://whd.northcantonschools.org/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets/mine/?username='. $username . '&password='. $password . '&limit=1000');
      
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
      
      curl_setopt($ch, CURLOPT_HEADER, false);
      
      //ignore invalid certificate (this should be fixed for security reasons, once a server is permanently setup for this site)
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      
      //execurte session
      $curl_data = curl_exec($ch);
      
      //close session
      curl_close($ch);
      
      //print results
      echo $curl_data;
      
      ?>
      
      
      
      

       

      Thank you for all the help, I am trying to do some leg work so I do not look like a complete idiot.
        • Re: Web Help Desk API, cURL PHP example? (PUT Data)
          libritech

          I've only dabbled in PHP, but here's what I use for issuing PUT requests to WHD:

           

          function rest_PUT($s_url, $s_jsonData){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $s_url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $s_jsonData);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/json',
              'Content-Length: ' . strlen($s_jsonData))
            );
          
            $s_response = curl_exec($ch);
            curl_close($ch);
          
            return $s_response;
          }// end rest_PUT()
          


          $s_url is the URL to the ticket plus any authentication, so in your case it would be:

          $s_url  = 'https://hd.northcantonschools.org/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets/ticket#/';
          $s_url .= '?username='. $username;
          $s_url .= '&password='. $password . '&limit=1000';
          


          $s_jsonData is the string returned by json_encode() of whatever values you are passing to WHD. I'm not sure what you would need in order to link assets to clients and tickets, but the following is an example of the JSON structure which we use to create new tickets.

           

          $a_ticketDetails = array(
            'subject' => 'This is the ticket subject',
            'detail' => 'This is the ticket detail',
            'problemtype' => array(
              'type' => 'ProblemType',
              'id' => 4
            ),
            'emailTech' => false,
            'emailClient' => true,
            'sendEmail' => true,
            'assignToCreatingTech' => false,
            'location' => array(
              'type' => 'Location',
              'id' => 5
            ),
            'clientReporter' => array(
              'type' => 'Client',
              'id' => 117
            )
          );
          
          
          $s_ticketDetails = json_encode($a_ticketDetails);
          

           

          Hope that helps. Let me know if any of it doesn't make sense and I'll try to elaborate.

           

          ~libri

            • Re: Web Help Desk API, cURL PHP example? (PUT Data)
              emguser

              IT WORKED!!!

               

              I was able to link a ticket with an asset!

              If anyone has any questions or I was not clear with my description please let me know and I will explain further.

               

              EDIT: (When I figure how to link a Client(student) with an asset(chromebook) I will post this as well.

              The project has been scrapped, hopefully what I have provided will help.)

               

              This is how I modified the code given by a generous user libritech.

               

              Small modification to function

              function rest_PUT($s_url, $s_jsonData){  
                $ch = curl_init();  
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //added, was not verifying with SSL certificate. I will need to work with my admin, but for now this works
                curl_setopt($ch, CURLOPT_URL, $s_url);  
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');  
                curl_setopt($ch, CURLOPT_POSTFIELDS, $s_jsonData);  
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(  
                  'Content-Type: application/json',  
                  'Content-Length: ' . strlen($s_jsonData))  
                );  
                
                $s_response = curl_exec($ch); 
                curl_close($ch);
                return $s_response ;   
              }// end rest_PUT()  <---by-the-way nice touch you do not see too many people that do this.
              

               

               

              the values are posted to the php script

              $ticket_id = $_POST['tid']

              Building the URL

              $s_url = 'https://hd.northcantonschools.org/helpdesk/WebObjects/Helpdesk.woa/ra/Tickets/';
              $s_url .= $ticket_id . '/';  
              $s_url .= '?username='. $username;  
              $s_url .= '&password='. $password . '&limit=1000';  
              

               

               

               

              Building Array/JSON

              $a_ticketDetails = array
                ( 
                'assets' => array
                     ( 
                     'id' => '3145', 
                     'type' => 'Asset', 
                     'assetNumber' => '39777' 
                     )
                );
              
              
              $s_ticketDetails = json_encode($a_ticketDetails);
              //s_ticketDetails (output) {"assets":{"id":"3145","type":"Asset","assetNumber":"39777"}}
              
              $s_ticketDetails = str_replace(":{", ":[{", $s_ticketDetails);
              $s_ticketDetails = str_replace("}}", "}]}", $s_ticketDetails);
              //notice the square brackets it seems that WHD needs this format to understand what is being PUT.
              //s_ticketDetails (new output) {"assets":[{"id":"3145","type":"Asset","assetNumber":"39777"}]}
              
              echo $s_ticketDetails . " <br />";
              
              echo rest_PUT($s_url, $s_ticketDetails);
              

               

              NOTE:

              the id is different from the asset so I will need to do a GET to search for asset then extract the ID and then build array/json

               

              'id' => '3145',

                     'type' => 'Asset',

                     'assetNumber' => '39777'

               

              https://hd.northcantonschools.org/helpdesk/WebObjects/Helpdesk.woa/ra/Assets/?assetNumber=39777&password=****&username=*…

              JSON output:

               

              [
                {
                  "id": 3145, // this is the ID I want. Please see my first example of how to extract the data from REST-GET / JSON.
                  "type": "Asset",
                  "assetNumber": "39777",
                  "contractExpiration": null,
                  "macAddress": null,
                  "networkAddress": null,
                  "networkName": null,
                  "notes": null,
                  "purchaseDate": null,
                  "serialNumber": "H******",
                  "version": "Dell Gen2",
                  "assetstatus": null,
                  "billingRate": null,
                  "location": {
                    "id": 3,
                    "type": "Location",
                    "address": "",
                    "city": "",
                    "locationName": "",
                    "postalCode": "",
                    "state": "OH",
                    "defaultPriorityTypeId": null
                  }
                }
              ]