2016-12-06 18 views
1

我使用php創建肥皂客戶端,我已成功添加標頭並調用該服務。使用本地php客戶端構建SOAP主體

我的問題是我的API與

應用程序發生錯誤,請檢查您的請求,並再次嘗試 迴應。

我用

htmlentities($client->__getLastRequest()) 

與輸出的XML進行比較和預期的XML,然後我發現以下

傳出

<SOAP-ENV:Body> 
<ns1:ping/> 
</SOAP-ENV:Body> 

預計

<S:Body> 
    <ns3:ping xmlns:ns2="http://www.example.com/example" xmlns:ns3="http://example.core.engine.tflip.uua.com/"> 
     <arg0> 
      <ns2:token>Wed Apr 06 01:19:24 IST 2016</ns2:token> 
     </arg0> 
    </ns3:ping> 
</S:Body> 

很顯然我送一個空的身體,但我不知道如何創建這些,因爲我使用

$params = array(
      "token" => 'Wed Apr 06 01:19:24 IST 2016' 
     ); 
$result = $client->__soapCall("ping", array($params)); 

調用服務,我需要建立上述結構,也有添加這些節點的名稱空間。

也請建議我是否會引起而不是定義的

請幫我在這的任何問題。

+0

使用'$ result = $ client - > __ soapCall(「ping」,$ params);'因爲$ params已經是數組 –

+0

@LukaSvalina是的,這已經是一個數組,但改變不會有幫助。裏面__soapCall我只需要傳遞一個數組。 – Darshan

回答

0

終於找到了一個適合我的解決方案,至少在需要的時候發佈。

有了所有的安全性頭文件,我創建了soap客戶端,幷包含接受用戶名和密碼的類。事實證明,wsdl它自己處理了ping方法的名稱空間。

所以我的代碼是這樣的。

使用wsdl創建soap客戶端並使其可跟蹤以便我可以使用htmlentities($ client - > __ getLastRequest());和htmlentities($ client - > __ getLastResponse());檢查我的請求和響應XML。注意已經使用htmlentities()使其在瀏覽器中可見。

$client = new SoapClient("--the wsdl link--", array('trace' => 1)); 

而且設置與類WsseAuthHeader我在哪裏開始的所有驗證和服務所需的頭肥皂頭。 WsseAuthHeader類擴展了SoapHeader。

$client->__setSoapHeaders(Array(new WsseAuthHeader("username", "password"))); 

使用創建的$ client以期望的值調用服務中的ping方法(操作)。

$response = $client->ping(array('arg0'=>array('token'=>'test','timestamp'=>(new DateTime())->format('Y.m.d H:i:s')))); 

終於可以做回聲測試與

echo htmlentities($client->__getLastResponse()); 

希望這會有所幫助,特別感謝爲@Marcel對他的支持輸出。

2

PHP SoapClient有時候有點混亂。最簡單的方法是使用對象。以下示例是無用的。

class Ping { 
    protected $arg0; 

    public function setArg(SoapVar $oArg0) { 
     $this->arg0 = $oArg; 
    } 

    public function encode() { 
     return new SoapVar(
      $this, 
      SOAP_ENC_OBJECT, 
      null, 
      null, 
      'ping', 
      'http://example.core.engine.tflip.uua.com/' 
     } 
    } 
} 

class Arg { 
    protected $token; 

    public function getToken() { 
     return $this->token; 
    } 

    public function setToken($oToken) { 
     if (!($oToken instanceof SoapVar)) { 
      $oToken = new SoapVar(
       $oToken, 
       XSD_STRING, 
       null, 
       null, 
       'token', 
       'http://www.example.com/example' 
      ); 
     } 

     $this->token = $oToken; 
    } 

    public function encode() { 
     return new SoapVar(
      $this, 
      SOAP_ENC_OBJECT, 
      null, 
      null, 
      'arg0' 
     ); 
    } 
} 

try { 
    // init your soap client with wsdl 
    $oClient = new SoapClient(...); 

    // init your arg object 
    $oArg = new Arg(); 
    $oArg->setToken('Wed Apr 06 01:19:24 IST 2016'); 
    $oArgEncoded = $oArg->encode(); 

    // init the ping object 
    $oPing = new Ping(); 
    $oPing->setArg($oArgEncoded); 
    $oPingEncoded = $oPing->encode(); 

    // call the ping method with soap encoded arg object 
    $oResult = $oClient->ping($oPingEncoded); 
} catch (SoapFault $oSoapFault) { 
    echo "<pre>"; 
    var_dump($oSoapFault); 
    echo "</pre>"; 
} 

首先,我們有令牌成員和getters和setter的arg對象。編碼函數將arg對象作爲完全編碼的SoapVar對象。所以你可以直接用soap客戶端調用websercive的ping方法。

+0

謝謝你的漂亮的代碼和解釋,試了一下,但仍然沒有幫助ping方法有空值。更具體地說,我所做的是將所有這些函數和try-catch部分包含在一個已有的類中,並將所有對象都更改爲$ this->。你能告訴我什麼價值將會爲$ this這樣做嗎(在Arg類中返回新的SoapVar($ this,SOAP_ENC_OBJECT,'arg0');)行? – Darshan

+0

try { $ client = new SoapClient('my WSDL Initiations'); $ client - > __ setSoapHeaders('my headers confg'); // $ oArg = new Arg(); //評論obj創作 $ this-> setToken('Wed Apr 06 01:19:24 IST 2016'); $ oArgEncoded = $ this-> encode(); $ oResult = $ client-> ping($ oArgEncoded); echo'請求:
'。 htmlentities($ client - > __ getLastRequest()); } catch(SoapFault $ oSoapFault){ echo「

"; var_dump(); echo "
」; } – Darshan

+0

No actually ..「My Fault」它必須是一個單獨的類...現在它發送正確的請求...雖然仍然是相同的問題...代碼發送arg0作爲一個xi:type,但在預期這是一個這將是一個問題? – Darshan