PHP with cURL

Simple example to retrieve information about an airframe, by registration
PHP 4.0 and higher. You need to have the cURL extension loaded. Click here for more info.

Code

<?php
    //Make a simple API call and return the result

    //setup variables to use in the API call
    $API_KEY ="TESTAPIKEY"; //Replace this with your own API KEY
    $API_PASSWORD ="TESTAPIKEYPASSWORD"; //Replace this with your own API PASSWORD
    $API_ACCESS_TOKEN ="TESTAPIACCESSTOKEN"; //Replace this with your own API ACCESS TOKEN
    $API_BASE_URL="https://www.avdelphi.com/api/1.0/";
    $API_ENDPOINT="airframes";
    $API_COMMAND="info";
    $REGISTRATION="HB-JNB";
    $ITEMLIST="type_short;type_long;manufacturer";

    $targetUrl=$API_BASE_URL . $API_ENDPOINT .".svc?api_key=$API_KEY&api_access_token=$API_ACCESS_TOKEN&cmd=$API_COMMAND&registration=$REGISTRATION&itemlist=$ITEMLIST";

    // Initialize session and set URL.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $targetUrl);
    // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,90);  //cURL connection timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 400); //cURL process timeout in seconds

    //make the call and get the result
    $PAGE_RAW = curl_exec($ch);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($PAGE_RAW, 0, $header_size);
    $RESULT = substr($PAGE_RAW, $header_size);
    curl_close($ch);

    //print the result
    echo print_r($RESULT,true);
?>
Copy to clipboard



Working Example

<html>
    <head>
        <title>AvDelphi API - PHP with cURL example</title>
    </head>

    <style>
        *
        {
            font-family: arial;
        }
    </style>

    <body>
        <h3>Calling the AvDelphi API with PHP and cURL</h3>
        <br>
        <div id='api_response' style='width: 100%; height: auto; min-height: 40vh; border: 1px solid lightgrey; font-family: courier,sans-serif;'>


          <?php
            //Make a simple API call and return the result

            $API_KEY ="TESTAPIKEY"; //Replace this with your own API KEY
            //$API_PASSWORD ="TESTAPIKEYPASSWORD"; //Replace this with your own API PASSWORD
            $API_ACCESS_TOKEN ="TESTAPIACCESSTOKEN"; //Replace this with your own API ACCESS TOKEN
            $API_BASE_URL="https://www.avdelphi.com/api/1.0/";
            $API_ENDPOINT="airframes";
            $API_COMMAND="info";
            $REGISTRATION="HB-JNB";
            $ITEMLIST="type_short;type_long;manufacturer";

            $targetUrl=$API_BASE_URL . $API_ENDPOINT .".svc?api_key=$API_KEY&api_access_token=$API_ACCESS_TOKEN&cmd=$API_COMMAND&registration=$REGISTRATION&itemlist=$ITEMLIST";

            // Initialize session and set URL.
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $targetUrl);
            // Set so curl_exec returns the result instead of outputting it.
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,90);  //cURL connection timeout in seconds
            curl_setopt($ch, CURLOPT_TIMEOUT, 400); //cURL process timeout in seconds

            //make the call and get the result - cURL will return the entire page as one string, including the header and the body
            $PAGE_RAW = curl_exec($ch);

            //get the size of the page header from the result
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            //split out the header section
            //$header = substr($PAGE_RAW, 0, $header_size);

            //get the body section of the result
            $RESULT_RAW = substr($PAGE_RAW, $header_size);
            curl_close($ch);

            if ($RESULT_RAW)
            {
                //convert the JSON raw text into a PHP Array
                $DATA=json_decode($RESULT_RAW,true);

                $display_text ="<h3>API Result</h3><br>";
                $display_text .="Manufacturer: " . $DATA["result"][ 0 ]["manufacturer"] . "<br>";
                $display_text .="Type: " . $DATA["result"][ 0 ]["type_long"] . "<br>";
                $display_text .="<hr>";

                //print the result
                $pretty_print=print_r($DATA,true);

                $pretty_print=str_replace(" ","&nbsp;",$pretty_print);
                $pretty_print=str_replace(PHP_EOL,"<br>",$pretty_print);

                echo $display_text . $pretty_print;
            }

          ?>

        </div>


    </body>
</html>
Copy to clipboard   Try it