Re: TaxonomySearchRequest using PHP
Hi
This is for those of you using PHP and interested in using the NBN Web services.
For some time now I have been playing around with the TaxonomySearchRequest query
$response = $client->call('GetSpeciesList', $query );
My first attempt was very crude and was based on unravelling
$Taxons = $response['Taxon'];
- it kind of worked but had too many errors (e.g. TVKs mixed up)
My next version was basically to flatten the response based on a version of the 'array_to_str' code I found on the web (http://www.nio.uos.de/doku/phpdoku/LIB/Utils.html). It was neater and basically the array was flattened as follows:
foreach($array as $key=>$val)
{
if(is_array($val)){
$str_of_array_keys_n_vals .= "[$key = {". array_to_str($val)."}] , ";
}
else{
$str_of_array_keys_n_vals .= "[$key=$val] , ";
}
}
$TaxonDetailString = substr($str_of_array_keys_n_vals, 0, strlen($str_of_array_keys_n_vals)-3);
Unfortunately I did not figure out how to deal with Synoym and LowerTaxa as they appeared as separate species. I also wanted to have greater control e.g. to identify 'badly formed'
My current version is a complete rewrite - has fewer lines, has more control and extracts a lot more information. My code still basically flattens the array returned by the query using the following code (you are free to use it):
$ztring = html_entity_decode($client->response);//this is the important step!!!!
settype($ztring, "string");
$ztring_array = explode("<", $ztring);
for ($z=4; $z<count($ztring_array); $z++){
$new_ztring = str_replace("nbntq:", "", $ztring_array[$z]);
$new_ztring = str_replace(">", "", $new_ztring);
//now have tidy strings
if(strpos($new_ztring,"SynonymList")===0){
$s=1; //switch on Synonym list
}
if(strpos($new_ztring,"/SynonymList")===0){
$s=0; //switch off Synonym list
}
if(strpos($new_ztring,"LowerTaxaList")===0){
$l=1; //switch on LowerTaxa list
}
if(strpos($new_ztring,"/LowerTaxaList")===0){
$l=0; //switch off LowerTaxa list
}
if(strpos($new_ztring,"Taxon taxonVersionKey")===0){
$t=0;
$tvk++;
}else{
$t++;
}
if($s==0 && $l==0 && $t==0){
$sp++;
$tvk=1;
}//now have unique identifiers for all the elements and switches for Synonym and LowerTaxa
if ($t==0){
$tvk_array = explode('"',$new_ztring);
$species_TVKs[$sp] = $species_TVKs[$sp].",".$tvk_array[1];
}//If badly formed
if ($t==1){
if (preg_match('/wellFormed="false"/', $new_ztring)){
$bf_TVK = substr($species_TVKs[$sp], -16);
$species_bf_TVKs[$sp] = $species_bf_TVKs[$sp].",".$bf_TVK;
$species_TVKs[$sp] = substr($species_TVKs[$sp], 0, -17);
$bf = 1; //Set the $bf switch
}else{
$bf = 0;
}
}if ($tvk==1){
if ($t==1){
$scientific_name_array = explode('"',$new_ztring);
$Scientific_Name[$sp] = $scientific_name_array[6];
}
if ($t==3){
$scientific_authority_array = explode("Authority",$new_ztring);
$Scientific_Authority[$sp] = $scientific_authority_array[1];
}
if ($t==5){
$category_array = explode("TaxonReportingCategory",$new_ztring);
$category[$sp] = $category_array[1];
}
}elseif ($bf==0){
if ($t==1){
$names_array = explode('"',$new_ztring);
if ($names_array[3]=="false"){
$species_common_names[$sp] = $species_common_names[$sp].", ".$names_array[6];
}else{
$species_names[$sp] = $species_names[$sp].", ".$names_array[6];
}
}
if ($t==3){
$authority_array = explode("Authority",$new_ztring);
$authority = $authority_array[1];
if(is_string($authority)&&!$species_names[$sp]==""){ //problem with non-printing characters
$species_names[$sp] = $species_names[$sp]."[".$authority."]";
}
}
}
}
It doesn't look much but it took me quite a while to figure out. I am still testing (http://www.nesbrec.org/thesaurus2.php) but it seems to be working a lot better. If anyone has suggestions for improvements or needs help, let me know
Cheers
Nick