如何使用 wse-php 库通过 SOAP 连接到安全的 Web 服务 [英] How to use wse-php library to connect to secured webservices via SOAP

查看:32
本文介绍了如何使用 wse-php 库通过 SOAP 连接到安全的 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,它应该使用 WS-Security 连接到网络服务.目前 - 在我的脚本中,我构建了一个 soap XML 并将其发送到 webservice 端点,但我收到了一个 xml 响应,上面写着客户端内部错误".

I have a script that is supposed to connect to webservice using WS-Security. Currently - in my script, I built a soap XML and sent it to the webservice endpoint but I'm getting an xml response saying "Client Internal Error".

这是我正在使用的代码:

Here's the code that I'm using:

<?php
function sendXMLRequest($url, $params)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/soap+xml"));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    ob_start();
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    if ($response === false || $info['http_code'] != 200) {
      $output = "No cURL data returned for $url [". $info['http_code']. "]";
      if (curl_error($ch))
        $output .= "\n". curl_error($ch);
      $response .= $output;
     }
    ob_end_clean();
    curl_close($ch);    
    return $response;
}
/* $currentTime = time();
$timestamp = gmdate('Y-m-d\TH:i:s', $currentTime).'Z';
$nonce = mt_rand();
$non = base64_encode(pack('H*',$nonce)); */
$username = 'derek';
$password = 'Momentum1';
$wsdl = "http://localhost/test/wsdl-src/CRMLeadService.wsdl";
$momurl = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";

echo("Post to URL: {$momurl}\n");

$xml = '<?xml version="1.0" encoding="utf-8"?>';
$xml .= '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.momentum.co.za/crm/service/application/CRMLeadService/v1.0" xmlns:v11="http://www.momentum.co.za/crm/service/type/application/Lead/v1.0" xmlns:v12="http://www.momentum.co.za/crm/service/type/TitleType/v1.0" xmlns:v13="http://www.momentum.co.za/crm/service/type/LanguageType/v1.0" xmlns:v14="http://www.momentum.co.za/crm/service/type/PreferredContactMethodType/v1.0" xmlns:v15="http://www.momentum.co.za/crm/service/type/CampaignType/v1.0" xmlns:v16="http://www.momentum.co.za/crm/service/type/ProductCategoryType/v1.0">';
$xml .= '<soapenv:Header>';
$xml .= '<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">';
$xml .= '<wsse:UsernameToken wsu:Id="UsernameToken-45">';
$xml .= '<wsse:Username>'.$username.'</wsse:Username>';
$xml .= '<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>';
$xml .= '</wsse:UsernameToken>';
$xml .= '</wsse:Security>';
$xml .= '</soapenv:Header>';
$xml .= '<soapenv:Body>';
$xml .= '<v1:CreateLeadRequest>';
$xml .= '<createLead>';
$xml .= '<v11:LeadSourceId>23627e70-a29e-e211-b8a8-005056b81ebe</v11:LeadSourceId>';
$xml .= '<v11:AffiliateLeadReference>852800020</v11:AffiliateLeadReference>';
$xml .= '<v11:Title>';
$xml .= '<v12:Code>852800018</v12:Code>';
$xml .= '</v11:Title>';
$xml .= '<v11:Initials>MD</v11:Initials>';
$xml .= '<v11:PreferredName>Marius</v11:PreferredName>';
$xml .= '<v11:FirstName>Marius</v11:FirstName>';
$xml .= '<v11:LastName>Drew</v11:LastName>';
$xml .= '<v11:PreferredCorrespondenceLanguage>';
$xml .= '<v13:Code>852800001</v13:Code>';
$xml .= '</v11:PreferredCorrespondenceLanguage>';
$xml .= '<v11:PreferredCommunicationMethod>';
$xml .= '<v14:Code>852800000</v14:Code>';
$xml .= '</v11:PreferredCommunicationMethod>';
$xml .= '<v11:Campaign>';
$xml .= '<v15:Code>95D9042A-440E-E311-A5EB-005056B81EA5</v15:Code>';
$xml .= '</v11:Campaign>';
$xml .= '<v11:HomePhoneNumber>0723621762</v11:HomePhoneNumber>';
$xml .= '<v11:BusinessPhoneNumber>0723621762</v11:BusinessPhoneNumber>';
$xml .= '<v11:MobilePhoneNumber>0723621762</v11:MobilePhoneNumber>';
$xml .= '<v11:EmailAddress>mdrew@gmail.com</v11:EmailAddress>';
$xml .= '<v11:Notes>IMU</v11:Notes><v11:ProductCategories>';
$xml .= '<v16:Code>d000083d-229c-e211-b8a8-005056b81ebe</v16:Code>';
$xml .= '</v11:ProductCategories>';
$xml .= '</createLead>';
$xml .= '</v1:CreateLeadRequest>';
$xml .= '</soapenv:Body>';
$xml .= '</soapenv:Envelope>';

echo $resp = sendXMLRequest($momurl, $xml);
?>

当我在互联网上搜索时,我发现了这个名为 wse-php,我想我绝对可以使用它.问题是,我不知道如何使用它.没有太多关于如何使用它的信息.也许这里有人已经使用过这个...请帮忙.谢谢.

When I was searching on the internet, I found this library called wse-php and I think I could definitely use this. The problem is, I don't know hoe to use it. There's not much information given on how to use it. Maybe someone here have already used this... please help. Thanks.

PS:如果需要,这里是 WSDL 和 XSD 文件的链接:

PS: Here's the link to the WSDL and XSD files if you need them:

http://sdrv.ms/16KC8o4

推荐答案

你需要这个:

https://gist.github.com/Turin86/5569152

它修改了 PHP 中内置的 SoapHeader 类以支持 WS-Security,包括 PasswordType(如果不是摘要).这是我用来使它工作的代码:

It modifies the SoapHeader class build into PHP to support WS-Security, including PasswordType (if not digest). This is the code I used to make it work:

//include the soap class before this call

$wsdl = "wsdl";
$momurl = "location";

//Perform Request
$username = '***';
$password = '***';
$client = new WSSoapClient($wsdl, array('location' => $momurl));
$client->__setUsernameToken($username,$password,'PasswordText');
$client->__setSoapHeaders($header);

try { 
    $result = $client-> //make soap call
} catch (Exception $e) { 
    $msgs = $e->getMessage();
    echo "Error: $msgs"; 
} 

如果您还需要什么,请告诉我.

If you need anything further, let me know.

这篇关于如何使用 wse-php 库通过 SOAP 连接到安全的 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆