Curl to php converter

Curl to php converter

Some vendors provide documentation on how to interact with their hardware using Curl.

If you have a curl command and want to convert it to php use the link below.

This link helped me to work out how to access HPE iLO 5 as it is not the same as iLO 4 or below

http://incarnate.github.io/curl-to-php/

Get Qumulo Details

Get Qumulo Details

				
					public function get_Qumulo_Details($IPv4Addr, $Username, $Password)
{

 $QM_Session_Key = '';
 $QM_Session_Key = get_session_key($IPv4Addr,$Username, $Password);
 $QM_Response = Get_Qumulo_Details($IPv4Addr, $QM_Session_Key, 'GET', '/v1/file-system');
 return $QM_Response;
}// END get_Qumulo_Details

public static function get_session_key($ipaddr,$uname, $pword) {

   $Session_Key = '';
   $data = '{"username": "' . $uname . '", "password": "' . $pword . '"}';
   $Data_Len = strlen($data);
   $wsapi_url = 'https://' . $ipaddr . ':8000/v1/session/login';

   $ch = curl_init();
   curl_setopt($ch,CURLOPT_URL,$wsapi_url);
   curl_setopt($ch, CURLOPT_HTTPHEADER, Array('POST /v1/session/login HTTP/1.1 -H'));
   curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Length: ' . $Data_Len));
   curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //FALSE outputs the responce to the page - TRUE outputs responce to as string
   curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
   curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

   $response = curl_exec($ch);

   curl_close($ch);
   // echo var_dump($response);

   $response = json_decode($response,true);
   foreach ($response as $key => $value)
        {
            $Session_Key = $value;
        }
   return $Session_Key;

}// END get_session_key

public function Get_Qumulo_Details($IPv4Addr, $Session_Key, $TYPE, $API_Dir)
{
    $wsapi_url = 'https://' . $IPv4Addr . ':8000' . $API_Dir;
    $data = 'Authorization: Bearer ' . $Session_Key;

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$wsapi_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array($TYPE . ' ' . $API_Dir . ' HTTP/1.1 -H'));
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //FALSE outputs the responce to the page - TRUE outputs responce to as string
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    $response = curl_exec($ch);

    curl_close($ch);
    // echo var_dump($response);

    $response = json_decode($response,true);

    return $response;
}
				
			

Get HPE 3PAR/Primera Details

Get HPE 3PAR/Primera Details

				
					// 3PAR System Details https://[3PAR IP]:8080/api/v1/system
// 3PAR cpg Details https://[3PAR IP]:8080/api/v1/cpgs
// 3PAR Hosts Details https://[3PAR IP]:8080/api/v1/hosts
// 3PAR Hostsets Details https://[3PAR IP]:8080/api/v1/hostsets
// 3PAR Volumes Details https://[3PAR IP]:8080/api/v1/volumes

public function get_StoreServ_Array_Details($IPv4Addr, $Username, $Password)
    {
      $comment = '';
      $contact = '';
      // Before you get any data from 3PAR you need to get a session key.
      $Session_Key = get_session_key($IPv4Addr, $Username, $Password);
      $API_Dir = "system";
      $SS_Array_Rest = new StoreServeRest;
      $SS_Response = Get_StoreServe_Details($IPv4Addr, $Session_Key, $API_Dir);
      $content = '';

      foreach($SS_Response as $key => $value)
      {
        if(is_array($value))
        {
            $value = 'ARRAY!';
        }

        if ($key == "name") {
            $name = $value;
        } elseif ($key == "systemVersion") {
            $systemVersion = $value;
        } elseif ($key == "patches") {
            $patches = $value;
        } elseif ($key == "IPv4Addr") {
            $IPv4Addr = $value;
        } elseif ($key == "model") {
            $model = $value;
        } elseif ($key == "serialNumber") {
            $serialNumber = $value;
        } elseif ($key == "totalNodes") {
            $totalNodes = $value;
        } elseif ($key == "masterNode") {
            $masterNode = $value;
        } elseif ($key == "onlineNodes") {
            $onlineNodes = $value;
        } elseif ($key == "clusterNodes") {
            $clusterNodes = $value;
        } elseif ($key == "chunkletSizeMiB") {
            $chunkletSizeMiB = $value;
        } elseif ($key == "totalCapacityMiB") {
            $totalCapacityMiB = $value;
            $totalCapacityGiB = $totalCapacityMiB / 1024;
            $totalCapacityTiB = $totalCapacityGiB / 1024;
        } elseif ($key == "allocatedCapacityMiB") {
            $allocatedCapacityMiB = $value;
            $allocatedCapacityGiB = $allocatedCapacityMiB / 1024;
            $allocatedCapacityTiB = $allocatedCapacityGiB / 1024;
        } elseif ($key == "freeCapacityMiB") {
            $freeCapacityMiB = $value;
            $freeCapacityGiB = $freeCapacityMiB / 1024;
            $freeCapacityTiB = $freeCapacityGiB / 1024;
        } elseif ($key == "failedCapacityMiB") {
            $failedCapacityMiB = $value;
        } elseif ($key == "contact") {
            $contact = $value;
        } elseif ($key == "comment") {
            $comment = $value;
        } elseif ($key == "timeZone") {
            $timeZone = $value;
        } elseif ($key == "licenseInfo") {
            $licenseInfo = $value;
        } elseif ($key == "parameters") {
            $parameters = $value;
        }
    }
}

function Get_StoreServe_Details($IPv4Addr, $Session_Key, $API_Dir)
{
     $contentType = "application/json -H";
     $wsapi_url = 'https://' . $IPv4Addr . ':8080/api/v1/' . $API_Dir;

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $wsapi_url);

     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('GET /api/v1/volumes HTTP/1.1')); // -H
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Accept: ' . $contentType));
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Accept-Language: en'));
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('X-HP3PAR-WSAPI-SessionKey: ' . $Session_Key));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //FALSE outputs the responce to the page - TRUE outputs responce to as string
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
     curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

     $response = curl_exec($ch);
     $response = json_decode($response,true);

     return $response;
}


// Before you get any data from 3PAR you need to get a session key.

public static function get_session_key($ipaddr,$uname, $pword) {
    $data = '{"user":"'. $uname. '","password":"'. $pword . '"}';
    $contentType = "application/json";
    $wsapi_url = 'https://' . $ipaddr . ':8080/api/v1/credentials';
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$wsapi_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('GET /api/v1/credentials HTTP/1.1 -H')); // -H
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Accept: ' . $contentType)); // -H
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //FALSE outputs the responce to the page - TRUE outputs responce to as string
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    $response = curl_exec($ch);

    curl_close($ch);

    $response = json_decode($response,true);

    foreach ($response as $key => $value) {
       return $value;
   }
}
				
			

Check for HPE iLO

Check for HPE iLO

				
					// Every HPE iLO will provide basic details without even logging in.
// http://[iLO IP Address]/xmldata?item=All
// iLO 7 onwards can use https
// I ran this against every IP on the iLO subnet

public function Get_ILO_Data($IPv4Addr)
    {
        $ProsessedOutput = '';
        $curl = curl_init();
        curl_setopt_array($curl, Array(
        CURLOPT_URL            => 'http://' . $IPv4Addr . '/xmldata?item=All',
        CURLOPT_USERAGENT      => 'spider',
        CURLOPT_TIMEOUT        => 15,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_ENCODING       => 'UTF-8'
        ));
        $Server_XML = curl_exec($curl);
        curl_close($curl);

        if(! $Server_XML)
        {
            // Use this to do what you want when no XMLdate returned.
            // Mark this IP as not HPE iLO
        }else{
            // Use this to do what you want when XMLdate returned.
            // Mark this IP as HPE iLO
        }
    }

public function Prosess_Server_Output($Server_XML, $IPv4Addr)
        {
            // Reset values so that they do not report false info.
            $HTML = '';
            $Product_Type = "";
            $Hardware_Type = "";
            $Serial =  "";
            $Product_ID =  "";
            $Description =  "";
            $Enclosure =  "";
            $Firmware =  "";
            $Hardware_Name  =  "";
            $ILO_Type =  "";
            $IRS =  "";
            $bay =  "";
            $sstatus = "";
            $isilo = "";
            $gen = 0;

            $Product_Type = getTextBetweenTags($Server_XML,"PN");
            $Hardware_Type = getTextBetweenTags($Server_XML,"SPN");
            $Description = getTextBetweenTags($Server_XML,"DESCRIPTION");

            if (strpos($Product_Type, 'c7000') !== false){
                $isilo = "True";
                $gen =2;
                $Serial = trim(getTextBetweenTags($Server_XML,"ENCL_SN"));
                $Hardware_Type = getTextBetweenTags($Server_XML,"PN");
                $Firmware = getTextBetweenTags($Server_XML,"FWRI");
                $Hardware_Name  = getTextBetweenTags($Server_XML,"ENCL");
                $ILO_Type = getTextBetweenTags($Server_XML,"PN");
                $IRS = getTextBetweenTags($Server_XML,"IRS");
                $Enclosure = getTextBetweenTags($Server_XML,"ENCL");
                $sstatus = getTextBetweenTags($Server_XML,"STATUS");

            }elseif (strpos($Hardware_Type, 'Gen9') !== false){
                $isilo = "True";
                $gen =9;
                $Serial =  trim($this->getTextBetweenTags($Server_XML,"SBSN"));
                $Hardware_Type = getTextBetweenTags($Server_XML,"SPN");
                $Product_ID = getTextBetweenTags($Server_XML,"PRODUCTID");
                $Description = getTextBetweenTags($Server_XML,"DESCRIPTION");
                $Enclosure = getTextBetweenTags($Server_XML,"ENCL");
                $Firmware = getTextBetweenTags($Server_XML,"FWRI");
                $Hardware_Name  = getTextBetweenTags($Server_XML,"SN");
                $IP_Address = getTextBetweenTags($Server_XML,"IPADDR");
                $ILO_Type = getTextBetweenTags($Server_XML,"PN");
                $IRS = getTextBetweenTags($Server_XML,"IRS");
                $bay = getTextBetweenTags($Server_XML,"BAY");
                $sstatus = getTextBetweenTags($Server_XML,"STATUS");
            }elseif (strpos($Hardware_Type, 'Gen8') !== false){
                $isilo = "True";
                $gen =8;
                $Serial = trim(getTextBetweenTags($Server_XML,"SBSN"));
                $Hardware_Type = getTextBetweenTags($Server_XML,"SPN");
                $Product_ID = getTextBetweenTags($Server_XML,"PRODUCTID");
                $Description = getTextBetweenTags($Server_XML,"DESCRIPTION");
                $Enclosure = getTextBetweenTags($Server_XML,"ENCL");
                $Firmware = getTextBetweenTags($Server_XML,"FWRI");
                $Hardware_Name  = getTextBetweenTags($Server_XML,"SN");
                $IP_Address = getTextBetweenTags($Server_XML,"IPADDR");
                $ILO_Type = getTextBetweenTags($Server_XML,"PN");
                $IRS = getTextBetweenTags($Server_XML,"IRS");
                $bay = getTextBetweenTags($Server_XML,"BAY");
                $sstatus = getTextBetweenTags($Server_XML,"STATUS");


            }elseif (strpos($Hardware_Type, 'G7') !== false){
                $isilo = "True";
                $gen =7;
                $Serial = trim(getTextBetweenTags($Server_XML,"SBSN"));
                $Hardware_Type = getTextBetweenTags($Server_XML,"SPN");
                $Firmware = getTextBetweenTags($Server_XML,"FWRI");
                $sstatus = getTextBetweenTags($Server_XML,"STATUS");
                $Description = "iLO 3";

            }elseif (strpos($Hardware_Type, 'G6') !== false){
                $isilo = "True";
                $gen =6;
                $Serial = trim(getTextBetweenTags($Server_XML,"SBSN"));
                $Hardware_Type = getTextBetweenTags($Server_XML,"SPN");
                $Firmware = getTextBetweenTags($Server_XML,"FWRI");
                $sstatus = getTextBetweenTags($Server_XML,"STATUS");
                $Description = "iLO 2";

            }elseif (strpos($Description, 'iLO 5') !== false){
                $isilo = "True";
                $gen =10;
                $Serial = trim(getTextBetweenTags($Server_XML,"SBSN"));
                $Hardware_Type = getTextBetweenTags($Server_XML,"SPN");
                $Firmware = getTextBetweenTags($Server_XML,"FWRI");
                $sstatus = getTextBetweenTags($Server_XML,"STATUS");

            }
        }

// This will return the content between tag <TAG>content</TAG>
// <SN>ILOCZ12345678</SN>

public function getTextBetweenTags($string, $tagname)
{
    $pattern = "/<$tagname>(.*?)<\/$tagname>/";
    preg_match($pattern, $string, $matches);
    if(!empty($matches))
    {
        $RTN = $matches[1];
    }else{
        $RTN = '';
    }
    return $RTN;
}
				
			

HPE iLO 4 & 3 Rest API Using php

HPE iLO 4 & 3 Rest API Using php

				
					public function Get_ILO4_Details($IPv4Addr, $user_name, $password)
{
    $IPv4Addr = '192.168.1.2';
    $user_name = 'Administrator';
    $password = 'iLO 5 Password';
    $contentType = "application/json";
    $contentType = "application/json";
    $wsapi_url = 'https://' . $IPv4Addr . '/rest/v1/Systems/1';

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$wsapi_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('GET /rest/v1/Systems/1 HTTP/1.1 -H')); // -H
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Accept: ' . $contentType)); // -H
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_USERNAME, $User_name);
    curl_setopt($ch, CURLOPT_PASSWORD, $Password);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //FALSE outputs the responce to the page - TRUE outputs responce to as string
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    $errors = curl_error($ch);
    $response = curl_exec($ch);
    $response = json_decode($response, true);

    return $response;
}
				
			

Working with json output

Working with json output

				
					// This is how I went through the json output to extract all the details.

public function ProsessIloData($arr, $IPv4Addr, $indent) {
    $Output = '';
        if ($arr) {
            foreach ($arr as $key => $value) {
            if (is_array($value)) {
                //
                ProsessIloData($value, $IPv4Addr, $indent . $key . '_');
            } else {
                //  Output
                $Output = $Output . "<br>Key: " . $indent . $key .  ' Value: ' . $value  . "<br>";
                // Remove the rem statment from the line below to show all the keys and the values.
                // echo "<br>Key: " . $key .  ' Value: ' . $value  . "<br>";

            // Create a new elseif statment for each key you want the value for.
            if($indent . $key == "AssetTag"){
                $AssetTag = '';
                $AssetTag = $value;
                Update_sql("`AssetTag`= '" . $AssetTag . "'", $IPv4Addr);
            }elseif($indent . $key == "BiosVersion"){
                $BiosVersion = '';
                $BiosVersion = $value;
                Update_sql("`BiosVersion`= '" . $BiosVersion . "'", $IPv4Addr);
            }
            }
        }
    }

}

// This is how I updated the database by using the IP as the WHERE
public function Update_sql($sql, $IPv4Addr){
    global $wpdb;
    $ilo_hardware_Table = $wpdb->prefix . 'ilo_hardware';
    $ilo_hardware_update_sql = "UPDATE " . $ilo_hardware_Table . " SET
                        " . $sql . "
                    WHERE `IP_Address` = '" . $IPv4Addr . "'";
    $wpdb->query($ilo_hardware_update_sql);
    if ($wpdb->last_error !== '') :

        $str = htmlspecialchars($wpdb->last_result, ENT_QUOTES);
        $query = htmlspecialchars($wpdb->last_query, ENT_QUOTES);

        print   "<div id='error'>
                            <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
                            <code>$query</code></p>
                        </div>";

    endif;
}
				
			

HPE iLO 5 Rest API Using php

HPE iLO 5 Rest API Using php

				
					public function Get_ILO5_Details($IPv4Addr, $user_name, $password, $API_Dir)
{
     $IPv4Addr = '192.168.1.2';
     $user_name = 'Administrator';
     $password = 'iLO 5 Password';
     $contentType = "application/json";
     $URL = 'https://' . $IPv4Addr . '/redfish/v1/' . $API_Dir;

     $ch = curl_init();

     curl_setopt($ch, CURLOPT_URL, $URL);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Accept: ' . $contentType));
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array('OData-Version: 4.0'));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
     curl_setopt($ch, CURLOPT_USERPWD, $User_name . ':' . $Password);

     $response = curl_exec($ch);
     if (curl_errno($ch)) {
         echo 'Error:' . curl_error($ch);
     }


     curl_close($ch);
     $response = json_decode($response, true);
     // This will dump the responce to the scree.
     // Good to do if it is the first time you have run this.
     // var_dump($response);

     echo '<br><br><br>';

     return $response;
}