无法使用SOAP和PHP连接到webservice

时间:2015-02-09 23:02:53

标签: php curl soap wsdl soap-client

我正在尝试连接到webservice API,但它无法正常工作。这是我第一次设置这样的东西,我不知道它为什么不起作用。

我的代码是

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);       

我得到以下结果......

  

致命错误:未捕获的SoapFault异常:[HTTP]无法连接到主机/home/brgwe507/public_html/previas/wp-content/plugins/sample-globalsight/sample-globalsight.php:31堆栈跟踪:#0 [内部函数]:SoapClient-> __ doRequest('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->登录('')#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)#home {home}抛出/ home / brgwe507 / public_html / previas / wp-content / plugins / sample-globalsight / sample-globalsight。第31行的PHP

我可以使用以下代码连接curl,但是SoapClient无法正常工作

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());

}

0 个答案:

没有答案