<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Forum — TaxonomySearchRequest using PHP]]></title>
		<link>https://forums.nbn.org.uk/viewtopic.php?id=650</link>
		<atom:link href="https://forums.nbn.org.uk/extern.php?action=feed&amp;tid=650&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in TaxonomySearchRequest using PHP.]]></description>
		<lastBuildDate>Fri, 23 May 2008 16:06:36 +0000</lastBuildDate>
		<generator>PunBB 1.4.6</generator>
		<item>
			<title><![CDATA[Re: TaxonomySearchRequest using PHP]]></title>
			<link>https://forums.nbn.org.uk/viewtopic.php?pid=2920#p2920</link>
			<description><![CDATA[<p>Hi</p><p>This is for those of you using PHP and interested in using the NBN Web services.</p><p>For some time now I have been playing around with the TaxonomySearchRequest query </p><div class="codebox"><pre><code>$response = $client-&gt;call(&#039;GetSpeciesList&#039;, $query );</code></pre></div><p>My first attempt was very crude and was based on unravelling </p><div class="codebox"><pre><code>$Taxons = $response[&#039;Taxon&#039;];</code></pre></div><p>- it kind of worked but had too many errors (e.g. TVKs mixed up)</p><p>My next version was basically to flatten the response based on a version of the &#039;array_to_str&#039; code I found on the web (<a href="http://www.nio.uos.de/doku/phpdoku/LIB/Utils.html">http://www.nio.uos.de/doku/phpdoku/LIB/Utils.html</a>). It was neater and basically the array was flattened as follows: </p><div class="codebox"><pre><code>foreach($array as $key=&gt;$val)
{
  if(is_array($val)){
  $str_of_array_keys_n_vals .= &quot;[$key = {&quot;. array_to_str($val).&quot;}] , &quot;;
  }
  else{
  $str_of_array_keys_n_vals .= &quot;[$key=$val] , &quot;;
  }
}
$TaxonDetailString = substr($str_of_array_keys_n_vals, 0, strlen($str_of_array_keys_n_vals)-3);</code></pre></div><p>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 &#039;badly formed&#039;</p><p>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):</p><div class="quotebox"><blockquote><p>$ztring = html_entity_decode($client-&gt;response);//this is the important step!!!!<br />settype($ztring, &quot;string&quot;); <br />$ztring_array = explode(&quot;&lt;&quot;, $ztring);<br />for ($z=4; $z&lt;count($ztring_array); $z++){<br />$new_ztring = str_replace(&quot;nbntq:&quot;, &quot;&quot;, $ztring_array[$z]);<br />$new_ztring = str_replace(&quot;&gt;&quot;, &quot;&quot;, $new_ztring);<br />//now have tidy strings<br />if(strpos($new_ztring,&quot;SynonymList&quot;)===0){<br />&nbsp; &nbsp;$s=1; //switch on Synonym list<br />&nbsp; &nbsp;}<br />if(strpos($new_ztring,&quot;/SynonymList&quot;)===0){<br />&nbsp; &nbsp;$s=0; //switch off Synonym list<br />&nbsp; &nbsp;}<br />if(strpos($new_ztring,&quot;LowerTaxaList&quot;)===0){<br />&nbsp; &nbsp;$l=1; //switch on LowerTaxa list<br />&nbsp; &nbsp;}<br />if(strpos($new_ztring,&quot;/LowerTaxaList&quot;)===0){<br />&nbsp; &nbsp;$l=0; //switch off LowerTaxa list<br />&nbsp; &nbsp;}<br />if(strpos($new_ztring,&quot;Taxon taxonVersionKey&quot;)===0){<br /> $t=0;<br /> $tvk++;<br /> }else{<br />&nbsp; $t++;<br />}<br />if($s==0 &amp;&amp; $l==0 &amp;&amp; $t==0){ <br />$sp++;<br />$tvk=1; <br />}</p><p>//now have unique identifiers for all the elements and switches for Synonym and LowerTaxa<br />if ($t==0){<br />$tvk_array = explode(&#039;&quot;&#039;,$new_ztring);<br />$species_TVKs[$sp] = $species_TVKs[$sp].&quot;,&quot;.$tvk_array[1];<br />}</p><p>//If badly formed<br />if ($t==1){ <br />&nbsp; if (preg_match(&#039;/wellFormed=&quot;false&quot;/&#039;, $new_ztring)){<br />$bf_TVK = substr($species_TVKs[$sp], -16);<br />$species_bf_TVKs[$sp] = $species_bf_TVKs[$sp].&quot;,&quot;.$bf_TVK;<br />&nbsp; $species_TVKs[$sp] = substr($species_TVKs[$sp], 0, -17); <br />&nbsp; $bf = 1; //Set the $bf switch<br />&nbsp; }else{<br />&nbsp; $bf = 0;<br />&nbsp; }<br />}</p><p>if ($tvk==1){<br />&nbsp; &nbsp; if ($t==1){<br />&nbsp; &nbsp; $scientific_name_array = explode(&#039;&quot;&#039;,$new_ztring);<br />&nbsp; &nbsp; $Scientific_Name[$sp] = $scientific_name_array[6];<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; if ($t==3){<br />&nbsp; &nbsp; $scientific_authority_array = explode(&quot;Authority&quot;,$new_ztring);<br />&nbsp; &nbsp; $Scientific_Authority[$sp] = $scientific_authority_array[1];<br />&nbsp; &nbsp; }<br />&nbsp; if ($t==5){ <br />&nbsp; $category_array = explode(&quot;TaxonReportingCategory&quot;,$new_ztring);<br />&nbsp; $category[$sp] = $category_array[1];<br />&nbsp; }<br />}elseif ($bf==0){ <br />&nbsp; &nbsp; if ($t==1){<br />&nbsp; &nbsp; $names_array = explode(&#039;&quot;&#039;,$new_ztring);<br />&nbsp; &nbsp; &nbsp; if ($names_array[3]==&quot;false&quot;){&nbsp; <br />&nbsp; &nbsp; &nbsp; $species_common_names[$sp] = $species_common_names[$sp].&quot;, &quot;.$names_array[6]; <br />&nbsp; &nbsp; &nbsp; }else{<br />&nbsp; &nbsp; &nbsp; $species_names[$sp] = $species_names[$sp].&quot;, &quot;.$names_array[6];<br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />&nbsp; &nbsp; if ($t==3){<br />&nbsp; &nbsp; $authority_array = explode(&quot;Authority&quot;,$new_ztring);<br />&nbsp; &nbsp; &nbsp; $authority = $authority_array[1];<br />&nbsp; &nbsp; &nbsp; if(is_string($authority)&amp;&amp;!$species_names[$sp]==&quot;&quot;){ //problem with non-printing characters<br />&nbsp; &nbsp; &nbsp; $species_names[$sp] = $species_names[$sp].&quot;[&quot;.$authority.&quot;]&quot;; <br />&nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br /> }<br />}</p></blockquote></div><p>It doesn&#039;t look much but it took me quite a while to figure out. I am still testing (<a href="http://www.nesbrec.org/thesaurus2.php">http://www.nesbrec.org/thesaurus2.php</a>) but it seems to be working a lot better. If anyone has suggestions for improvements or needs help, let me know </p><p>Cheers</p><p>Nick</p>]]></description>
			<author><![CDATA[null@example.com (Nick)]]></author>
			<pubDate>Fri, 23 May 2008 16:06:36 +0000</pubDate>
			<guid>https://forums.nbn.org.uk/viewtopic.php?pid=2920#p2920</guid>
		</item>
	</channel>
</rss>
