PHP guide

NOTE: The minimum PHP Version required to run this script is PHP 5.4

  • To execute this self-contained script, you must set Authentication Key:

    $basic_auth = "your-authentication-key";
    
  • Script APISearchRequest.php

<?php

$basic_auth = "your-authentication-key";

$argv = $_SERVER['argv'];
$searchKeyword = $argv[1];
if(empty($searchKeyword)){
    echo "Error : Required argument - Search keyword was not specified\n";
    return;
}

$api_endpoint = "https://graph.ir.ee/organizations?legal-name=" . $searchKeyword;

$response = searchAPIRequest ($api_endpoint, $basic_auth);
echo "--------------searchAPIRequest Response------------------------\n";
var_dump($response);
echo "\n--------------end searchAPIRequest Response------------------------\n";

/**
 * Process response - You can get the values you need in the response, below is an example
 */
$parsedResult = json_decode ($response);
$items = $parsedResult->result->items;
$organizationData = array ();

foreach ($items as $item) {
    echo "_about      >> " . $item->_about ."\n";
    echo "legalName   >> " . $item->legalName ."\n";

    /** Making a request to a linked resource from the results, e.g "https://graph.ir.ee/organizations/ee-11735006"*/
    $organizationData[$item->_about] = searchAPIRequest ($item->_about, $basic_auth);
}
echo "---------Organizations Data----------\n";
var_dump ($organizationData);
echo "\n---------end Organizations Data----------\n";

/** other fields in the response */
echo $parsedResult->result->itemsPerPage . "\n";
echo $parsedResult->result->page . "\n";
echo $parsedResult->result->startIndex . "\n";
echo $parsedResult->result->totalResults . "\n";

if (!empty($parsedResult->result->next)) {
    echo $parsedResult->result->next . "\n";
    $nextPageResponse = searchAPIRequest ($parsedResult->result->next, $basic_auth);
    echo "---------next page response----------\n";
    var_dump ($nextPageResponse);echo "\n";
}

function searchAPIRequest ($api_endpoint, $basic_auth)
{
    $ch = "";
    $headers = array (
        'Content-Type:application/json',
        'Authorization: Basic ' . $basic_auth);

    try {
        $ch = curl_init ();
        curl_setopt ($ch, CURLOPT_URL, $api_endpoint);
        curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt ($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

        $response = curl_exec ($ch);
        $status_code = curl_getinfo ($ch, CURLINFO_HTTP_CODE);

        if ($status_code == 200) {

            return $response;

        } else {
            throw new Exception("An error has occurred with code : " . $status_code . ", error message >> " . $response);
        }
    } catch (Exception $e) {
        echo $e->getMessage ();
    }

    curl_close ($ch);

}
  • To run the script:

    php APISearchRequest.php searchKeyword
    

    On successful response, you can process the json result as required. The content of $parsedResult->result is as below, showing only relevant part

    {
      "result":{
        "items":[ 
         {  
                "_about":"https://graph.ir.ee/organizations/ee-80326715",
                "legalName":"VENE RAHVAKULTUURIKESKUS BEREGINJA MTÜ"
             },{...}
        ],
          "itemsPerPage":50,
          "next":"https://graph.ir.ee/organizations.json?_page=1&legal-name=regi",
          "page":0,
          "startIndex":1,
          "totalResults":159,
      }
    }
    

Related articles

http://php.net/manual/en/ref.curl.php