Location: PHPKode > projects > Taxonomic Search Engine > funindex_wrapper.php
<?php

// $Id: funindex_wrapper.php,v 1.1.1.1 2005/05/19 10:31:10 rdmpage Exp $

/**
 * @brief Wrapper to talk to Index Fungorum using its web service
 *
 * Index Fungorum is a Microsoft .NET web service, which needs special care when using Nusoap:
 * -# Need to set namespace in parameters (ns1:)
 * -# Need to set method namespace and SOAPAction value in call method
 *
 * Failure to set the namespace in the paramaters results in an empty pair of NewDataSet tags. 
 * Failure to set namespace and SOAPAction value  results in a
 * "Server did not recognize the value of HTTP Header SOAPAction: " error.  Much time spent 
 * figuring this out. The debugging code and M/Gateway Developments WSDL validator service
 * (<A HREF="http://www.mgateway.com/WSDLClient.htm">http://www.mgateway.com/WSDLClient.htm</A>) 
 * were of great help, as I could see how my SOAP messages differed from M/Gateway's 
 * (which worked first time).
 *
 */
class FunindexWrapper extends Wrapper {
	
	/**
	 * Store the address of the WSDL file for this service.
	 * @protected
	 */
	var $wsdl;

	
	//----------------------------------------------------------------------------
	/**
	 * Sets the WSDL address for the service 
	 * (currently <A HREF="http://www.indexfungorum.org/ixfwebservice/fungus.asmx?WSDL">http://www.indexfungorum.org/ixfwebservice/fungus.asmx?WSDL</a>),
	 * and sets the authority and namespace.
	 */
	function FunindexWrapper ()
	{	
		$this->wsdl = "http://www.indexfungorum.org/ixfwebservice/fungus.asmx?WSDL";
		
		$this->authority = "indexfungorum.org";
		$this->namespace = "RecordID";
		
		$this->xml = "";
		$this->StartXML();		
	}
	
	//----------------------------------------------------------------------------
	/**
	 * @brief Tests if Index Fungorum is alive.
	 *
	 * Uses the IsAlive method of the Index Fungorum web service to test if service is running.
	 *
	 * @return true if service is live, false otherwise
	 */
	function IsAlive ()
	{
		$result = false;
		
		global $config;
		
		// Create an empty array for the parameters (the IsAlive method does not take parameters
		$parameters[] = '';
				
		// Proxy settings
		$proxyhost = '';
		$proxyport = '';
		if ($config['proxy_name'] != '')
		{
			$proxyhost = $config['proxy_name'];
			$proxyport = $config['proxy_port'];
		}
		if ($config['soap_proxy_name'] != '')
		{
			$proxyhost = $config['soap_proxy_name'];
			$proxyport = $config['soap_proxy_port'];
		}
				
		// Create SOAP client from WSDL
		$soapclient = new soapclient($this->wsdl);
		$soapclient->debug_flag=true;

		$soapclient->setHTTPProxy ($proxyhost, $proxyport);

		// Make the call
		$result = $soapclient->call(
			'IsAlive',
			$parameters,
			'http://Cabi/FungusServer/', 		// method namespace
			'http://Cabi/FungusServer/IsAlive'	// SOAPAction value
			);
			
		//--------------------------------------------------------------------------------------------
		// Check for a fault
		if ($soapclient->fault)
		{
		    echo '<p><b>Fault: ';
		    print_r($result);
		    echo '</b></p>';
		} else
		{
		    // Check for errors
		    $err = $soapclient->getError();
		    if ($err)
		    {
		        // Display the error
		        echo '<p><b>Error: ' . $err . '</b></p>';
		    }
		}	
		//echo '<xmp>'.$soapclient->request.'</xmp>';
		//echo '<xmp>'.$soapclient->response.'</xmp>';
		
		return $result;
	}

	
	//----------------------------------------------------------------------------
	/**
	 * @brief Return details for a single record in Index Fungorum, using their SOAP interface
	 *
	 * @param id Index Fungorum of taxon name to retrieve
	 * @return Result in my XML format
	 */
	function GetDataForID ($id)
	{
		$result = "";
		
		global $config;
		
		$funindex_xml = $this->GetDataFromCache ("GetDataForID", "xml", $id);
		if ($funindex_xml == "")
		{
			// Get fresh data

		
			// Search parameters. Vital that we use "ns1:" prefix for parameter names
			
			$parameters = array(
				'ns1:NameKey'=> $id,
				);
			
			//print_r ($parameters);
			
			// Proxy settings
			$proxyhost = '';
			$proxyport = '';
			if ($config['proxy_name'] != '')
			{
				$proxyhost = $config['proxy_name'];
				$proxyport = $config['proxy_port'];
			}
			if ($config['soap_proxy_name'] != '')
			{
				$proxyhost = $config['soap_proxy_name'];
				$proxyport = $config['soap_proxy_port'];
			}
					
			// Create SOAP client from WSDL
			$soapclient = new soapclient($this->wsdl);
			$soapclient->soap_defencoding = 'ISO-8859-1'; //'UTF-8';
	
			$soapclient->debug_flag=true;
	
			$soapclient->setHTTPProxy ($proxyhost, $proxyport);
	
			// Make the call
			$this->StartTimer();
	
			$result = $soapclient->call(
				'NameByKey',
				$parameters,
				'http://Cabi/FungusServer/', 			// method namespace
				'http://Cabi/FungusServer/NameByKey'	// SOAPAction value
				);
				
			$this->StopTimer();
				
			//--------------------------------------------------------------------------------------------
			// Check for a fault
			if ($soapclient->fault)
			{
			    echo '<p><b>Fault: ';
			    print_r($result);
			    echo '</b></p>';
			} else
			{
			    // Check for errors
			    $err = $soapclient->getError();
			    if ($err)
			    {
			        // Display the error
			        echo '<p><b>Error: ' . $err . '</b></p>';
			    }
			    else
			    {
			    	// Note that tags have _x0020_ where column names in the database have a space
			    	
			    	// Add the XML header with UTF-8 character encoding
			    	$funindex_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
					$funindex_xml .= $result;
					
					// Deal with '&' linking author names
					$funindex_xml .= str_replace (" & ", " &amp; ", $xml);
					
					$this->StoreDataInCache ("GetDataForID", "xml", $id, $funindex_xml);
				}
			}
		}
		
		if ($funindex_xml != "")
		{
			// Transform Index Fungorum XML into our format
			
			// Tell XSLT processor the time used to search this service
			$params = array("timeused" => $this->time_used);				
			
			$xslt_file = "xsl/funindex_taxon.xsl";
			if ($config['sabcmd'] != '')
			{	
				// Transform using external sablot processor
				$xpresult = XSLT_Buffer ($funindex_xml, $xslt_file, '', $params);
				
				// If we have an error then bail out
				if (strpos ( $xpresult,"[code:" ))
				{
					$this->Error ("XSLT", $xpresult);
					$this->EndXML();
					return $this->xml;
				}
						
			}
			else
			{
				// Use XSLT extension
				$xslt_processor = xslt_create();
				$xslt = join ("", file($xslt_file));
				$arg_buffer = array("/xml" => $funindex_xml, "/xslt" => $xslt);
				$xp = xslt_create() or die ("Could not create XSLT processor");
				if (!($xpresult = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer, $params)))
				{
					echo "An error occurred: " . xslt_error($xp) . "(error code " . xslt_errno($xp) . ")";
				}
				xslt_free($xp);
			}		
	
			$result = $xpresult;
	    }
				
		
		return $result;
	}


	//----------------------------------------------------------------------------
	/**
	 * @brief Searches Index Fungorum for a name.
	 *
	 * The Index Fungorum web service returns an XML document (without header). This document
	 * is transformed into my form using XSLT.
	 *
	 * Here a pseudocode view of a search request:
	 *
	 * <pre>
	 *	NameSearch
	 *	(
	 *	   string SearchText = "Peltigera neopolydactyla",
	 *	   boolean AnywhereInText = 0,
	 *	   int MaxNumber = 1
	 *	)
	 * </pre>
	 *
	 * @param name The taxon name to search for
	 * @param qualifier Kind of search (default is exact)
	 * @param max_results The maximum number of records to return (default is 1)
	 */
	function NameSearch ($name, $qualifier = EXACT, $max_results = 1) 
	{
		$result = "";
		
		global $config;
		
		$id = nameToSafe ($name);
		
		$funindex_xml = "";
		$this->StartTimer();
		
		$funindex_xml = $this->GetDataFromCache ("NameSearch", "xml", $id);
		if ($funindex_xml == "")
		{
			// Search parameters. Vital that we use "ns1:" prefix for parameter names
			$anywhere = true;
			if ($qualifier == EXACT) { $anywhere = false; }
			
			$parameters = array(
				'ns1:SearchText'=> $name,
				'ns1:AnywhereInText' => $anywhere,
				'ns1:MaxNumber' => $max_results);
			
			//print_r ($parameters);
			
			// Proxy settings
			$proxyhost = '';
			$proxyport = '';
			if ($config['proxy_name'] != '')
			{
				$proxyhost = $config['proxy_name'];
				$proxyport = $config['proxy_port'];
			}
			if ($config['soap_proxy_name'] != '')
			{
				$proxyhost = $config['soap_proxy_name'];
				$proxyport = $config['soap_proxy_port'];
			}
					
			// Create SOAP client from WSDL
			$soapclient = new soapclient($this->wsdl);
			// Vital to ensure that if name has diacritic marks
			// we send these to the SOAP server correctly
			$soapclient->soap_defencoding = 'UTF-8';
	
			$soapclient->debug_flag=true;
	
			$soapclient->setHTTPProxy ($proxyhost, $proxyport);
	
			// Make the call
	
			$result = $soapclient->call(
				'NameSearch',
				$parameters,
				'http://Cabi/FungusServer/', 			// method namespace
				'http://Cabi/FungusServer/NameSearch'	// SOAPAction value
				);
				
				
			//--------------------------------------------------------------------------------------------
			// Check for a fault
			if ($soapclient->fault)
			{
			    echo '<p><b>Fault: ';
			    print_r($result);
			    echo '</b></p>';
			} else
			{
			    // Check for errors
			    $err = $soapclient->getError();
			    if ($err)
			    {
			        // Display the error
			        echo '<p><b>Error: ' . $err . '</b></p>';
			    }
			    else
			    {
		    		// Add the XML header with UTF-8 character encoding
			    	$funindex_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
					$funindex_xml .= $result;
					
					// Deal with '&' linking author names
					$funindex_xml .= str_replace (" & ", " &amp; ", $xml);
					
					$this->StoreDataInCache ("NameSearch", "xml", $id, $funindex_xml);
			    }
		    }
	    }
	    $this->StopTimer();
	    
	    if ($funindex_xml != "")
	    {			    	
			// Transform Index Fungorum XML into our format
			
			// Tell XSLT processor the time used to search this service
			$params = array("timeused" => $this->time_used);				
			
			$xslt_file = "xsl/funindex.xsl";
			if ($config['sabcmd'] != '')
			{	
				// Transform using external sablot processor
				$xpresult = XSLT_Buffer ($funindex_xml, $xslt_file, '', $params);
				
				// If we have an error then bail out
				if (strpos ( $xpresult,"[code:" ))
				{
					$this->Error ("XSLT", $xpresult);
					$this->EndXML();
					return $this->xml;
				}
						
			}
			else
			{
				// Use XSLT extension
				$xslt_processor = xslt_create();
				$xslt = join ("", file($xslt_file));
				$arg_buffer = array("/xml" => $funindex_xml, "/xslt" => $xslt);
				$xp = xslt_create() or die ("Could not create XSLT processor");
				if (!($xpresult = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer, $params)))
				{
					echo "An error occurred: " . xslt_error($xp) . "(error code " . xslt_errno($xp) . ")";
				}
				xslt_free($xp);
			}		
	
			$result = $xpresult;
		   
		}			
		
		return $result;
	}

}

?>
Return current item: Taxonomic Search Engine