Connect webservice with SOAP in PHP

10

I am trying to connect to the webservice but it is not working. The wsdl address is link

PHP code:

<?php
if (!class_exists('SoapClient'))
{
    die ("You haven't installed the PHP-Soap module.");
}

$clientSoap = new SoapClient( 
                "http://91.205.172.97/globalsight/services/AmbassadorWebService?wsdl", 
                array( 
                    'username'  =>  'teste123',
                    'password'  =>  'teste123.'
                )
              );

$params = array( 'username'     =>  'teste123',
                 'password'  =>  'teste123.' );

$result = $clientSoap->login( $params );
print_r($result);    

?>

The answer:

Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host in /home/brgwe507/public_html/previas/wp-content/plugins/sample-globalsight/sample-globalsight.php:31 Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://localhos...', '', 1, 0)
#1 /home/brgwe507/public_html/previas/wp-content/plugins/sample-globalsight/sample-globalsight.php(31): SoapClient->__call('login', Array)
#2 /home/brgwe507/public_html/previas/wp-content/plugins/sample-globalsight/sample-globalsight.php(31): SoapClient->login('')
#3 /home/brgwe507/public_html/previas/wp-includes/plugin.php(496): sample_globalsight('')
#4 /home/brgwe507/public_html/previas/wp-admin/admin.php(212): do_action('sample_globalsi...', Array)
#5 {main} thrown in /home/brgwe507/public_html/previas/wp-content/plugins/sample-globalsight/sample-globalsight.php on line 31

Thanks for everyone's help so far. I forgot to put the code that is working with the curl. With this code I can connect to the webservice, but I can not do anything else because when I try to make another call with the generated token I get the message "stream closed":

    function sample_globalsight(){

class soap_client{

    public $xmlRequest;
    public $header;

    function set_header(){
        $this->header = array(
            "Content-type: text/xml;charset=\"utf-8\"",
            "Accept: text/xml",
            "Cache-Control: no-cache",
            "Pragma: no-cache",
            "SOAPAction: \"\"",
            "Content-length: ".strlen($this->xmlRequest)
        );
    }

    function send(){
        $this->set_header();
        $soapCURL = curl_init();
        curl_setopt($soapCURL, CURLOPT_URL, "http://91.205.172.97/globalsight/services/AmbassadorWebService?wsdl" );
        curl_setopt($soapCURL, CURLOPT_CONNECTTIMEOUT, 100);
        curl_setopt($soapCURL, CURLOPT_TIMEOUT,        1000);
        curl_setopt($soapCURL, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($soapCURL, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($soapCURL, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($soapCURL, CURLOPT_POST,           true );
        curl_setopt($soapCURL, CURLOPT_POSTFIELDS,     $this->xmlRequest);
        curl_setopt($soapCURL, CURLOPT_HTTPHEADER,     $this->header);
        //Executing Curl Here.
        $result = curl_exec($soapCURL);
        if($result === false) {
          $err = 'Curl error: ' . curl_error($soapCURL);
          $result = $err;
          //echo "This is text".$err;
        }
        curl_close($soapCURL);
        return $result;
    }
}
$data ='<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.globalsight.com/webservices/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <p_username xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">teste123</p_username>
         <p_password xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">teste123.</p_password>
      </web:login>
   </soapenv:Body>
</soapenv:Envelope>';
$obj = new soap_client();
$obj->xmlRequest = $data;
print_r($obj->send());

} 
    
asked by anonymous 10.02.2015 / 00:13

1 answer

2

I took a look at WSDL and it seems to me that the correct way to use this method would be:

<?php
// Tente desabilitar o cache da WSDL
ini_set('soap.wsdl_cache_enabled',0); 
ini_set('soap.wsdl_cache_ttl',0);

$client = new SoapClient('http://91.205.172.97/globalsight/services/AmbassadorWebService?wsdl');
try {
    $client->login('teste123', 'teste123.');
} catch (SoapFault $excp) {
    // Tratar exceção
}

The SoapClient class exposes a method called __getFunctions that returns an array with the signature of the available methods.

    
15.02.2015 / 19:28