2016-08-16 97 views
0

我想从一个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(); 

回答

0

我是新和不知道我的职位将是有益的或没有,但我会尽力帮助你 - 如果你已经得到了你的WSDL文件或URL然后

- 你可以使用PHP SoapClient的

  • 善意忽略,如果后不符合要求

尝试按照下面的例子给出,也是你的WSDL文件结构(控股通过XML 功能和参数)

它给你,可以把步骤1和步骤2的代码类/函数里面,或者可以以简单的给定方式使用。

步骤1-

尝试 {

$客户=新SoapClient的(SOAP_CLIENT_WSDL_URL,阵列( 'soap_version'=> SOAP_VERSION, '跟踪'=>真)); return $ client;

}赶上($的SOAPFault故障){

trigger_error( “SOAP故障:(的faultcode:{断层$>的faultcode},faultstring:{$断层> faultstring})”,E_USER_ERROR);

die();

}

步骤2-

结果= $客户机 - > your_wsdl_containing_function( “follow_wsdl_parameter”)

$ xml_last_response = $客户端 - > __ getLastResponse()获得;

echo''; print_r($ xml_last_response);

+0

您可以更详细地了解步骤2吗?我必须结合step1和step 2吗? – Jim

+0

你可以把第1步代码放在initialize_soap_client()里面,然后在第2步你可以看到它被调用,然后可以使用对象soapclient调用wsdl里面的函数,最后你会收到你的最终响应/ xml,然后转换xml响应到数组中 – Pankaj

+0

现在已经更新了答案,没有initialize_soap_clie nt(),你可以用简单的方式使用它,还建议你在last_response之前也看到你的request_response,以便调试 – Pankaj