ChEBI Web Services - Making a PHP SOAP Client
1
0
Entering edit mode
5.5 years ago
support ▴ 40

On the ChEBI Web Services page: https://www.ebi.ac.uk/chebi/webServices.do

They give two examples of SOAP Clients for retrieving data from ChEBI, one of them is written in Java, one of them written in Perl. Heres the Perl example:

#!/usr/bin/perl -w
# SOAP::Lite version 0.67
# Please note: ChEBI webservices uses document/literal binding

use SOAP::Lite + trace => qw(debug);
#use SOAP::Lite;

# Setup service
my $WSDL = 'http://www.ebi.ac.uk/webservices/chebi/2.0/webservice?wsdl';
my $nameSpace = 'http://www.ebi.ac.uk/webservices/chebi';
my $soap = SOAP::Lite
   -> uri($nameSpace)
   -> proxy($WSDL);

# Setup method and parameters
my $method = SOAP::Data->name('getCompleteEntity')
                   ->attr({xmlns => $nameSpace});
my @params = ( SOAP::Data->name(chebiId => 'CHEBI:15377'));

# Call method
my $som = $soap->call($method => @params);

# Retrieve for example all ChEBI identifiers for the ontology parents
@stuff = $som->valueof('//OntologyParents//chebiId');
print @stuff;

The language I know best is PHP, so I want to convert this example into PHP. I'm new to the whole SOAP thing, so I barely know where to start. Heres what I started with:

$WSDL = 'http://www.ebi.ac.uk/webservices/chebi/2.0/webservice?wsdl';
$nameSpace = 'http://www.ebi.ac.uk/webservices/chebi';

$options = array(
    'uri'=>$nameSpace,
    'trace'=>true,
    'encoding'=>'UTF-8',
    'exceptions'=>true,
  );

/* Initialize webservice with your WSDL */
$client = new SoapClient($WSDL,$options);

/* Set your parameters for the request */
$params = array(
  'chebiId' => 'CHEBI:15377',
);

/* Invoke webservice method with your parameters */
$response = $client->__soapCall("getCompleteEntity", array($params));

/* Print webservice response */
var_dump($response);

Heres the output of that: http://pastebin.com/g3w0wf6u

So the things I'm wondering are:

1.) What do I do with the namespace? In the perl example, they setup the service like this:

my $soap = SOAP::Lite
   -> uri($nameSpace)
   -> proxy($WSDL);

How do I use the namespace with PHP SoapClient? I added it as the 'uri' option when setting up the webservice, but I don't actually know what that means. And they setup the method like this:

# Setup method and parameters
my $method = SOAP::Data->name('getCompleteEntity')
                   ->attr({xmlns => $nameSpace});

I see I need to apply it to the methods also. How do I setup a new method with the namespace applied, as shown in the Perl example?

What does proxy and uri mean in the perl example?

Also how can I replicate this:

@stuff = $som->valueof('//OntologyParents//chebiId');

With the SoapClient?

chebi soap api php • 3.5k views
ADD COMMENT
0
Entering edit mode
5.5 years ago
support ▴ 40

It was actually very easy, heres the code for the PHP SOAP client I made:

http://www.ebi.ac.uk/webservices/chebi/2.0/webservice?wsdl';
  public $nameSpace = 'http://www.ebi.ac.uk/webservices/chebi';


  /* Initialize webservice with your WSDL */
  public $options;
  public $client;

  public function __construct() {

    $this->options = array(
      'uri'=>$this->nameSpace,
      'trace'=>true,
      'encoding'=>'UTF-8',
      'exceptions'=>true,
    );

    $this->client = new SoapClient($this->WSDL,$this->options);


  }

  //  --------------------- EXAMPLE OF FUNCTION WHICH USES ONE OF THE WEBSERVICE METHODS -------------------------  //

  public function getAllChildren($chebiId,$relationshipType = 'is a',$structure = false) {

    /* Set your parameters for the request */
    $params = array(
      'chebiId' => $chebiId,
      'relationshipType' => $relationshipType,
      'structureOnly' => $structure
    );

    $data = $this->callSOAP('getAllOntologyChildrenInPath',$params);

    return $data;

  }

  //  --------------------- SOAP SETUP FUNCTIONS -------------------------  //

  public function callSOAP($method,$params) {

    try
      {
        $response = $this->client->__soapCall($method,array($params),array('uri' => $this->nameSpace));

        $data = $response->return;

        if (isset($data->ListElement)) { $data = $data->ListElement; }

        $data = convertToArray($data);

        return $data;
      } 
      catch(SoapFault $ex){
          echo $ex->getMessage();
      }

  }

}

To use it, do something like this:

$chebi = new SOAP_Tools();

$params = array(
    'search' => 'biological role',
    'searchCategory' => 'ALL',
    'maximumResults' => 50,
   'stars' => 'ALL'
);

$data = $chebi->callSOAP('getLiteEntity',$params);

I threw in the convertToArray function because its a pain in the ass working with an array of objects that it returns by default, so I added that to convert the output into a pure array.

ADD COMMENT

Login before adding your answer.

Traffic: 2223 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6