簡體   English   中英

如何使用wsHTTPBinding創建php soap wsdl客戶端?

[英]How to create php soap wsdl client with wsHTTPBinding?

我正在嘗試從wsdl中創建一個PHP客戶端,該客戶端使用WCF服務。 我已經通過僅通過憑據(不需要pem認證)來使用C#控制台應用程序使用了Web服務。

wsdl的網址如下(它是一個測試環境):

https://extenavigator.ukho.gov.uk/serviceB2B/submitUKHOOrdering.svc?wsdl

因此,如果有人想創建一個肥皂客戶端,請使用此wsdl。

“嘗試使用缺少相關服務權限的帳戶進行訪問將導致響應並顯示ProcessResult.Failure確認,並且向以下消息的Messages屬性添加了一條消息:服務[serviceName]對用戶[userId]不可用” (來自提供商)。

因此,工作肥皂客戶端使用錯誤的憑據應返回上述消息。

為了在php中創建肥皂客戶端,我首先通過wsdl2phpgenerator創建了存根類。 然后我實例化soap客戶端,如下所示:

ini_set('max_execution_time', 480);
ini_set('default_socket_timeout', 400);

$orderServiceClient = new OrderingService(array('login' => DIST_B2B_USERNAME, 'password' => DIST_B2B_PASSWORD, "trace"=>1, 
"exceptions"=>1, 'cache_wsdl' => WSDL_CACHE_MEMORY, 'soap_version' => SOAP_1_2, 'keep_alive' => false, 
"connection_timeout" => 240, 'verifypeer' => false, 'verifyhost' => false, "ssl_method" => SOAP_SSL_METHOD_TLS));

其中,OrderingService是擴展SOAP客戶端類的存根類。

最后,我調用這樣的方法:

$getHoldingsRequest = new GetHoldingRequest(DIST_ID, 25555, ProductType::AVCSCharts); // set some params for the called method

$responce = $orderServiceClient->GetHoldings($getHoldingsRequest); // call the method

我得到的錯誤是:提取HTTP標頭時出錯

我已經在php.ini和apache conf文件中啟用了ssl。 我正在使用Windows PC。

我已閱讀以下帖子:

使用PHP連接到受WS-Security保護的Web服務

SoapFault異常:[HTTP]提取HTTP標頭時出錯

我發現問題是必須自定義soap標頭才能傳遞憑據(“通過SSL的用戶名和密碼身份驗證”)

我也嘗試創建自定義的肥皂標題:

 class WsseAuthHeader extends SoapHeader 
{
    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
    function __construct($user, $pass, $ns = null) 
    {    
    if ($ns) 
    {        
        $this->wss_ns = $ns;    
    }    

    $auth = new stdClass();    

    $auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);     
    $auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);    
    $username_token = new stdClass();    
    $username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);     
    $security_sv = new SoapVar(        
                            new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns),        
                            SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);    

    parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }
}

 $wsse_header = new WsseAuthHeader("xxxx", "xxxx");

 $orderServiceClient = new OrderingService(array('trace' => true, 'exceptions' => true, 'cache_wsdl' => WSDL_CACHE_MEMORY, 
'soap_version' => SOAP_1_2, 'keep_alive' => false, 'connection_timeout' => 240));

 $orderServiceClient->__setSoapHeaders(array($wsse_header));

最后

$getHoldingsRequest = new GetHoldingRequest(DIST_ID, 25555, ProductType::AVCSCharts);
 $getHoldingsRequest->setRequestId($GUID);

try {
$responce = $orderServiceClient->GetHoldings($getHoldingsRequest);

} catch (Exception $e) {
echo $e->getMessage();
}


 echo "<pre>" . var_export($orderServiceClient->__getLastRequest(), TRUE) . "</pre>";

我得到:

提取HTTP標頭時出錯

空值

我也嘗試了以上帖子中提到的其他操作,但沒有結果。

從上面提到的帖子中:

“但是正如我上面所說:我認為需要更多有關WS-Security規范和給定服務體系結構的知識才能使此工作正常進行。”

因此,如果某人有使用肥皂客戶端的經驗,並且能夠為此wsdl創建php客戶端,那將是很大的幫助。

對於GUID,我使用了以下功能:

  function getGUID(){
if (function_exists('com_create_guid')){
    return com_create_guid();
}else{
    mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
    $charid = strtoupper(md5(uniqid(rand(), true)));
    $hyphen = chr(45);// "-"
    $uuid = chr(123)// "{"
        .substr($charid, 0, 8).$hyphen
        .substr($charid, 8, 4).$hyphen
        .substr($charid,12, 4).$hyphen
        .substr($charid,16, 4).$hyphen
        .substr($charid,20,12)
        .chr(125);// "}"
    return $uuid;
   }
}

   $GUID = getGUID();

我是新手,不確定我的文章會不會對您有幫助,但是我會盡力幫助您-如果您已經有了WSDL文件或URL,那么

-您可以使用php SoapClient

  • 如果發帖不符合您的要求,請忽略

嘗試遵循下面的示例以及您的WSDL文件結構(通過XML保留函數和參數)

它由您決定,可以將第1步和第2步代碼放入類/函數中,或者可以以簡單的給定方式使用。

第1步-

嘗試{

$ client = new SoapClient(SOAP_CLIENT_WSDL_URL,array('soap_version'=> SOAP_VERSION,'trace'=> true)); 返回$ client;

}捕獲(SoapFault $ fault){

trigger_error(“ SOAP錯誤:(錯誤代碼:{$ fault-> faultcode},錯誤字符串:{$ fault-> faultstring})”,E_USER_ERROR);

死();

}

第2步-

結果= $ client-> your_wsdl_ contains_function(“ follow_wsdl_parameter”)

$ xml_last_response = $ client-> __ getLastResponse();

回聲''; print_r($ xml_last_response);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM