You did not provide any parameters in the URL. We therefore provide the default service, which is the automatic documentation of this tool (the Generic Query Tool). '; // if the tool's URL is called without any parameters, we return the // documentation in TAPIR XML format. $output_string .= ' Generic Query Tool '; $output_string .= ' 0.001 beta '; $output_string .= ' bart.meganck@africamuseum.be '; $output_string .= ' The Generic Query Tool allows you to contact most types of data servers in TDWG/BioCASE context. You talk to this tool with HTTP GET statements, and you get the answers back in a TAPIR response XML. The minimal information you need to provide is the URL of the server you want to contact, in the "endpoint" variable. This will give you the information about that server (protocol, data schema, ...). For more functionality, see the parameter descriptions. '; $output_string .= ' endpoint '; $output_string .= ' op '; $output_string .= ' timeout '; $output_string .= ' start '; $output_string .= ' limit '; $output_string .= ' filter '; $output_string .= ' count '; $output_string .= ' responseschema '; } else { //non-empty parameter string // If parameters are passed, the endpoint parameter should be there if ( !isset($_GET['endpoint']) || empty($_GET['endpoint']) ) { $error_string .= ' "endpoint" parameter not specified, or empty. This parameter is required. If you want a short manual for this tool, simply point your browser at the base URL : '.$GQT_server_url.''; } else { //ok : endpoint provided $server_endpoint = urldecode($_GET['endpoint']); // check endpoint syntax, add "http://" if necessary if (substr($server_endpoint,0,7) != "http://") { $error_string .= ' The URL you specified in the "endpoint" parameter did not begin with "http://". We will add "http://" automatically, but if that does not work you should check your URL again : "'.$server_endpoint.'" '; $server_endpoint = "http://".$server_endpoint; } $req_destination = $server_endpoint; // now check all other (optional) parameters if (isset($_GET['op'])) { $operation_name = urldecode($_GET['op']); } else { $error_string .= ' An "endpoint" parameter was given without an "op" (operation) parameter. We therefore use the default operation "getEndpointInfo". If you want a short overview of all parameters, simply point your browser at the base URL of this tool : '.$GQT_server_url.''; $operation_name = 'getEndpointInfo'; } if (isset($_GET['timeout'])) { $request_timeout = urldecode($_GET['timeout']); } ini_set('default_socket_timeout',$request_timeout); if (isset($_GET['start'])) { $req_start = urldecode($_GET['start']); } if (isset($_GET['limit'])) { $req_limit = urldecode($_GET['limit']); } if (isset($_GET['filter'])) { $req_filter = urldecode($_GET['filter']); } if (isset($_GET['count'])) { $req_count = urldecode($_GET['count']); } if (isset($_GET['responseschema'])) { $req_response_schema = urldecode($_GET['responseschema']); } // at this point we have a (hopefully valid) endpoint, so we send a request // to get the endpoint's information. In case of the "getEndpointInfo" // operation, that's all we need. For search operations, this will provide // the basic information needed for constructing the query string. // Build the HTTP POST datastring, with a single (and empty) parameter $post_data = http_build_query( array('request' => '') ); $post_options = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $post_data ) ); // Build context and try server for response, catch any exceptions $post_context = stream_context_create($post_options); try { $response = file_get_contents($server_endpoint, false, $post_context); } catch (Exception $error1) { $error_string .= ''; } // Try to put the response in an XML object, catch any errors try { $response_xml = new SimpleXMLElement($response); } catch (Exception $error2) { $error_string .= ' Server response could not be interpreted as XML. Usually, this is due to a bad URL, or a server that is not (or too slowly) responding. Try (1) checking the URL. (2) If the URL seems ok, try pointing at it in another browser window : does the server respond ? how long does it take ? (3) If the server does respond (only a bit slow), set the "timeout" parameter a bit higher. If the server does NOT respond, well then this tool cannot do its job properly, can it ? '; } // Get namespaces from XML response, determine server type from namespaces if (isset ($response_xml)){ $communication_namespace = $response_xml->getDocNamespaces(); // TODO : response gives back xsi schemas as well : filter them out ! foreach ($communication_namespace as $ns_key => $ns_value){ if (strpos($ns_value,'digir') <> 0) { $server_type = 'digir'; $server_namespace = $ns_value; } if (strpos($ns_value,'biocase') <> 0) { $server_type = 'biocase'; $server_namespace = $ns_value; } if (strpos($ns_value,'tapir') <> 0) { $server_type = 'tapir'; $server_namespace = $ns_value; } // TODO : change this to a switch, add default action // ("namespace could not be found") } // foreach // See which operation is wanted, act accordingly switch ($operation_name) { case ('getEndpointInfo') : // General info $output_string .= ''.$server_type.''; $output_string .= ''.$server_endpoint.''; $output_string .= ''.$server_namespace.''; // DiGIR-only info if ($server_type=="digir") { foreach($response_xml->content->metadata->provider->resource as $resource) { $output_string .= ''.$resource->code.''; } $output_string .= '' .$response_xml->content->metadata->provider->resource->conceptualSchema .''; } // digir server_type if ($server_type=="biocase") { // Get all possible error messages and warnings foreach ($response_xml->diagnostics->diagnostic as $diagnostic) { $output_string .= '' .$diagnostic.''; } // Get all supported schemas foreach ($response_xml->content->capabilities->SupportedSchemas as $supp_schema) { foreach ($supp_schema->attributes() as $schema_key => $schema_value) { if ($schema_key == "namespace") { $output_string .= '' .$schema_value.''; } // if } // foreach attribute } // foreach supported schema } // biocase server type break; // case getEndpointInfo case ('search') : // Construction of search request switch ($server_type) { case ('biocase') : $myBiocaseRequest = new Biocase_request(); $req_string = $myBiocaseRequest->generateBiocaseRequest($req_GQT_version, $req_destination, $req_start, $req_limit, $req_response_schema, $req_filter, $req_count); break; // biocase case ('digir'): $request_catalogNumberTag = "CatalogNumber"; $myDigirRequest = new Digir_request(); echo("digir"); $req_string = $myDigirRequest->generateDigirRequest($req_resource, $req_destination, $req_filter, $req_start, $req_limit, $req_catalogNumberTag); break; //digir case ('tapir'): // TODO : implement TAPIR search query break; default : // no default operation break; } // switch server_type // Fill POST string for search request $search_post_data = http_build_query( array('request' => $req_string) ); $search_post_options = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $search_post_data ) ); // Build context and try server for response, catch any exceptions $search_post_context = stream_context_create($search_post_options); try { $search_response = file_get_contents($server_endpoint, false, $search_post_context); } catch (Exception $error3) { $error_string .= ''; } // Build context and try server for response, catch any exceptions try { $search_response_xml = new SimpleXMLElement($search_response); } catch (Exception $error4) { $error_string .= ' Server response could not be interpreted as XML. Usually, this is due to a bad URL, or a server that is not (or too slowly) responding. Try (1) checking the URL. (2) If the URL seems ok, try pointing at it in another browser window : does the server respond ? how long does it take ? (3) If the server does respond (only a bit slow), set the "timeout" parameter a bit higher. If the server does not respond at all, well then this tool cannot help it, can it ? '; } // Extract response content from protocol, write to output $output_string .= $search_response_xml->content->asXML(); $output_string .= $search_response_xml->content->count->asXML(); $diagnostic_string .= 'Got search response from the endpoint server. The response content is available here under the "'.$operation_name.'" tag.'; break; } // switch (operation) } // if isset $response_xml } //endpoint is provided } // parameter string not empty // Put the response and/or error messages into a TAPIR response XML echo(''); echo(''); echo('
'); echo(''); echo($GQT_server_url); echo(''); echo(''); echo(''); echo('
'); echo('<'.$operation_name.'>'); echo($output_string); echo(''); echo(' '); echo(''); echo($error_string); echo(''); echo($diagnostic_string); echo(''); echo('
'); ?>