我想從一個wsdl,使用WCF服務的PHP客戶端。 我已經設法使用C#控制檯應用程序使用Web服務,只通過憑據(不需要pem認證)。如何使用wsHTTPBinding創建php soap wsdl客戶端?
的WSDL的URL是以下(這是一個測試環境):
https://extenavigator.ukho.gov.uk/serviceB2B/submitUKHOOrdering.svc?wsdl
所以如果有人想嘗試創建一個SOAP客戶端使用該WSDL。
「嘗試訪問缺少相關服務權限的帳戶時,會導致響應並顯示ProcessResult.Failure確認,並將消息添加到以下消息屬性中:服務[serviceName]不可用於用戶[userId]「(來自提供者)。
因此,具有錯誤憑據的工作肥皂客戶端應返回上述消息。
爲了在php中首先創建soap客戶端,我通過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。
我看了這個帖子:
Connecting to WS-Security protected Web Service with PHP
SoapFault exception: [HTTP] Error Fetching http headers
我弄清楚,問題是,肥皂頭必須以憑據傳遞定製(「用戶名和密碼認證通過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頭
NULL
我也嘗試過其他的東西我提到n以上帖子沒有結果。
從上述職位:
「但正如我上面說:我認爲需要有關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();
您可以更詳細地瞭解步驟2嗎?我必須結合step1和step 2嗎? – Jim
你可以把第1步代碼放在initialize_soap_client()裏面,然後在第2步你可以看到它被調用,然後可以使用對象soapclient調用wsdl裏面的函數,最後你會收到你的最終響應/ xml,然後轉換xml響應到數組中 – Pankaj
現在已經更新了答案,沒有initialize_soap_clie nt(),你可以用簡單的方式使用它,還建議你在last_response之前也看到你的request_response,以便調試 – Pankaj