Connect the webservice with SOAP in PHP

Asked

Viewed 10,326 times

10

I’m trying to connect to the webservice but it’s not working. wsdl address is http://91.205.172.97/globalsight/services/AmbassadorWebService?wsdl

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 all your help so far. I forgot to put the code that is working with Curl. With this code I can connect to the webservice, but I can’t 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());

} 
  • The address really needs authentication ?

  • I tried with and without authentication and gave the same error in both cases.

  • @Ricardolimagonçalves by what I read in error SoapClient->__doRequest('<?xml version="...', 'http://localhos...', '', 1, 0) I think you’re trying to connect to localhost, which is strange. Is there any server redirection going on http://91.205.172.97?

  • @Guilhermenascimento found this strange too. As it is the first time I use this type of api I thought it was normal this. In the xml generated by http://91.205.172.97/globalsight/services/AmbassadorWebService?wsdl the second information appears <wsdl:service name="AmbassadorService">&#xA;<wsdl:port binding="impl:AmbassadorWebServiceSoapBinding" name="AmbassadorWebService">&#xA;<wsdlsoap:address location="http://localhost:8080/globalsight/services/AmbassadorWebService"/>&#xA;</wsdl:port>&#xA;</wsdl:service>.

  • That’s what I was just about to comment on. So this line is weird. I believe that in the documentation of this Webservice you have to define the domain and redirect port (theory). Or it’s a BUG (their configuration flaw). Which service is this, has documentation?

  • I may be wrong, but I think it’s some kind of proxy and you might want to disable it, see: https://github.com/tingley/globalsight/blob/master/main6/envoy/src/webserviceClients/ambassador-desktop/desktopicon/src/globalsight/www/webservices/AmbassadorServiceLocator.java#L25

  • It is the API of Globalsight, an open source translation and management system. [http://www.globalsight.com/wiki/index.php/GlobalSight_Web_Services_API]

  • I will change the localhost pro IP and recompile to see if it works...

  • I added Curl code to the question. I’m not sure if it helps...

  • @Ricardolimagonçalves You disabled the IP filter?

  • @Qmechanic73 did disable it... to test if it was really disabled, I reactivated it and made the Curl call by a server that was not in the list and was blocked. Then I deactivated it again.

Show 7 more comments

1 answer

2

I took a look at the 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 class SoapClient exposes a method called __getFunctions which returns an array with the signature of the available methods.

  • Edison, I edited your question to enter the code that was in Pastebin. This way we guarantee that the code will always be available here if something happens to the external site.

  • @Edson, thank you, but it didn’t work out. Same error: 'Soapfault Exception: [HTTP] Could not connect to host in /home/brgwe507/public_html/previas/wp-content/plugins/sample-globalsight/sample-globalsight.php:22`. I can connect with Curl, but I need the token that returns from login to call other functions and when I try to do Ava call with Curl using this token I get the closed stream message.

  • @Edson, about the filter has yes and I even disabled to test. But if that was the case I could not connect with Curl tb...

  • @Ricardolimagonçalves at some point the WSDL URL has changed? Try to disable the WSDL cache and run again. I edited my answer with these adjustments

  • @Edsonjosélimajunior apache proxy was redirecting to localhost, and generated this output in wsdl <wsdl:service name="AmbassadorService"> <wsdl:port binding="impl:AmbassadorWebServiceSoapBinding" name="AmbassadorWebService"> <wsdlsoap:address location="http://localhost:8080/globalsight/services/AmbassadorWebService"/> </wsdl:port> </wsdl:service> but now it’s ok. The URL itself hasn’t changed. I had already tried to disable the cache by loading the class and it hadn’t worked. It didn’t work either with init_set... I’m working on the Ws source code to see if I can figure out the problem.

  • I added the code I can connect with Curl to the question... I don’t know if it helps anything...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.