spacer
spacer

file_get_contents

For REST services using HTTP GET and/or POST the PHP function file_get_contents can be used to access the service.

HTTP GET

For services available through a HTTP GET request, for example WSDbfetch:

// Base URL for the service
$baseUrl = 'http://www.ebi.ac.uk/Tools/webservices/rest/dbfetch';
// Parameters for call
$params = array('db' => 'UNIPROT',
		'id' => 'ADH1A_HUMAN',
		'format' => 'fasta'
		);
// Build URL
$url = "$baseUrl/" . $params['db'] . '/' . $params['id'];
if(array_key_exists('format', $params)) $url .= '/' . $params['format'];
// Get entry
$result = file_get_contents($url);
// Print result
echo $result;

HTTP POST

For services which require the use of a HTTP POST request, for example WSInterProScan (see REST tutorial):

// Base URL for the service
$baseUrl = 'http://www.ebi.ac.uk/Tools/webservices/rest/';
// Tool name
$toolName = 'iprscan';
 
// Parameters for call
$params = array(
		'tool' => 'iprscan',
		'srchType' => 'interactive',
		'email' => 'your@email',
		'seqtype' => 'P',
		'crc' => TRUE,
		'sequence' => 'uniprot:slpi_human'
		);
 
// Create a context for the query to perform a POST
$postdata = http_build_query($params);
$opts = array('http' =>
	      array(
		    'method'  => 'POST',
		    'header'  => 'Content-type: application/x-www-form-urlencoded',
		    'content' => $postdata
		    )
	      );
$context  = stream_context_create($opts);
 
// Submit job
$result = file_get_contents($baseUrl . 'submit', FALSE, $context);
// Print result
echo "$result\n";

Examples

 
tutorials/06_programming/php/rest/file_get_contents.txt · Last modified: 2010/02/09 20:58 by hpm
spacer
spacer