1

Topic: Basic PHP REST example

For anyone struggling with a PHP implementation of REST here is something very crude, but seemingly working(?), that I've thrown together. It uses the HTTP_Request2 PEAR package which will need installing if you don't already have it. (Probably just a few clicks on your ISP's control panel, though I had to fiddle with the include path afterwards to get it included).

I had to override the certificate check which fails otherwise. No idea why, and well beyond my capabilitiies to understand such things! Maybe someone can advise on this.

Better implementations/suggestions welcome pleased. With the current dearth of PHP-based examples I'm just sticking my head over the parapet in the hope that having a target will draw the fire of better solutions, including industrial-grade coding...

(I'm abandoning pure JavaScript attempts as this will expose username and password required to get data(?)).

<?php

//===============
// Uses the PEAR HTTP_Request2 package
// This bit includes the necessary code. Your PHP include path may need fiddling with...
//===============
require_once "PEAR.php";
require_once "HTTP/Request2.php";


$verify = 0; // Gateway currently failing certificate test(?!). Setting this to zero ignores this.

//===============
// Login
//===============
$cmnd = 'https://data.nbn.org.uk/api/user/login';
$args = array('username' => 'XXXXXXXX',
              'password' => 'XXXXXXXX'
             );
$request = new HTTP_Request2($cmnd, HTTP_Request2::METHOD_GET, array('use_brackets' => true));
$request->setConfig('ssl_verify_peer', $verify);
$url = $request->getUrl();
$url->setQueryVariables($args);
$response = $request->send();

//echo $response->getBody();

$nbn_cookie = "";
foreach ($response->getCookies() as $c)
  {
  if ($c['name'] == 'nbn.token_key')
    {
    $nbn_cookie = $c['value'];
    //echo "Found NBN Cookie = " . $nbn_cookie . "<br/>";
    }
  }
if ($nbn_cookie == "") { exit("NBN cookie not found! Aborting further execution."); }


//===============
// Example Request (using 'GET'). Gets Clouded Yellow records in TL01E
//===============
$cmnd = 'https://data.nbn.org.uk/api/taxonObservations';
$args = array('ptvk'    => 'NHMSYS0000501953',
              'gridRef' => 'TL01E'
             );
$request = new HTTP_Request2($cmnd, HTTP_Request2::METHOD_GET, array('use_brackets' => true));
$request->addCookie('nbn.token_key', $nbn_cookie);
$request->setConfig('ssl_verify_peer', $verify);
$url = $request->getUrl();
$url->setQueryVariables($args);
$response = $request->send();

echo $response->getBody();

//===============
// Logout
//===============
$cmnd = 'https://data.nbn.org.uk/api/user/logout';
$request = new HTTP_Request2($cmnd, HTTP_Request2::METHOD_GET, array('use_brackets' => true));
$request->addCookie('nbn.token_key', $nbn_cookie);
$request->setConfig('ssl_verify_peer', $verify);
$url = $request->getUrl();
$response = $request->send();

//echo $response->getBody();

?>

Regards, Keith

2 (edited by kbalmer 15-01-2014 20:40:12)

Re: Basic PHP REST example

A couple more tips to add to this:

1) When specifying a list in REST,  e.g. for datasets to search, these need to be supplied to the Gateway like this

https://data.nbn.org.uk/api/taxonObservations/species?datasetKey=GA000458&datasetKey=GA000676

which means they can't go into the associative array $args (as you can only specify one value) so they have to be included within $cmnd instead. (In fact I now think the $args bit is unnecessary and you can probably just put the entire REST command into $cmnd).

If you are passing your list of datasets into an intermediate piece of PHP, e.g. via XMLHttpRequest, then you can't get at all the datasetKey values via the $_GET array either as you'll only get the last value, so I had to join them with ":", pass them in a single parameter, then split them in the PHP before correctly forming $cmnd.

(What a pain - REST lists couldn't be more inconvenient!)

2) A second lesson learned (and probably soon forgotten) is that although I was replacing the spaces in the POLYGON string with %20 when passing parameters to my intermediate PHP, these were being turned back into spaces when I read the $_GET array, and when they were then passed into HTTP_Request2 via $cmnd it simply refused to do anything sensible, and the error messages were pretty useless, just telling me that http version 1.1 wasn't supported!

i.e. you need to pass like this into HTTP_Request2

polygon=POLYGON((-0.3503534033106781%2051.997834595015185,-0.364585319046196%2052.00702465921265,-0.37947245637000204%2051.998238874001814,-0.3503534033106781%2051.997834595015185))

So make sure that you do something like

$cmnd = str_replace(" ", "%20", $cmnd);

just before invoking HTTP_Request2

Share and enjoy,
Keith