Web 服务 SOAP 请求适用于 SOAPUI,但不适用于 PHP
Posted
技术标签:
【中文标题】Web 服务 SOAP 请求适用于 SOAPUI,但不适用于 PHP【英文标题】:Web Service SOAP Request is working on SOAPUI but not working on PHP 【发布时间】:2013-09-30 11:56:27 【问题描述】:我已经尝试了几乎所有我能找到的可能的解决方案,但我似乎无法让这个脚本正常工作。以下是我之前的一些尝试:
How to use wse-php library to connect to secured webservices via SOAP
https://***.com/questions/18952222/soap-error-encoding-object-has-no-createlead-property-php
How to solve Error: SOAP-ERROR: Encoding: object has no 'createLead' property?
How to Consume a secured web service with WS-Security via SOAP [PHP]?
这是我最近的尝试:
<?php
try
$username = 'derek';
$password = 'Momentum1';
$url = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";
$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">
<soapenv:Header>
<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">
<wsse:UsernameToken>
<wsse:Username>derek</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Momentum1</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<v1:CreateLeadRequest>
<createLead>
<v11:LeadSourceId>23627e70-a29e-e211-b8a8-005056b81ebe</v11:LeadSourceId>
<v11:AffiliateLeadReference>852800020</v11:AffiliateLeadReference>
<v11:Title>
<v12:Code>852800018</v12:Code>
</v11:Title>
<v11:Initials>MD</v11:Initials>
<v11:PreferredName>Marius</v11:PreferredName>
<v11:FirstName>Marius</v11:FirstName>
<v11:LastName>Drew</v11:LastName>
<v11:PreferredCorrespondenceLanguage>
<v13:Code>852800001</v13:Code>
<v13:Description>?</v13:Description>
</v11:PreferredCorrespondenceLanguage>
<v11:PreferredCommunicationMethod>
<v14:Code>852800000</v14:Code>
</v11:PreferredCommunicationMethod>
<v11:Campaign>
<v15:Code>95D9042A-440E-E311-A5EB-005056B81EA5</v15:Code>
</v11:Campaign>
<v11:HomePhoneNumber>0723621762</v11:HomePhoneNumber>
<v11:BusinessPhoneNumber>0723621762</v11:BusinessPhoneNumber>
<v11:MobilePhoneNumber>0723621762</v11:MobilePhoneNumber>
<v11:EmailAddress>mdrew@gmail.com</v11:EmailAddress>
<v11:Notes>IMU</v11:Notes>
<v11:ProductCategories>
<v16:Code>d000083d-229c-e211-b8a8-005056b81ebe</v16:Code>
</v11:ProductCategories>
</createLead>
</v1:CreateLeadRequest>
</soapenv:Body>
</soapenv:Envelope>';
$content = utf8_encode($xml);
$content_length = strlen($xml);
$headers = array('Content-Type: application/soap+xml; charset=utf-8', 'Content-Length: ' . $content_length);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
print $output;
catch (Exception $e)
var_dump($e);
?>
我认为我发送信息的方式有问题。问题可能出在我的 WSDL 及其位置上吗?
我已经为此工作了一周,我已经感到沮丧了。这个 WS-Security 的东西很痛苦。我尝试在 *** 上使用此处的建议,例如创建一个扩展到 SoapClient 的类,但仍然无法正常工作。我希望有人可以成为天使并帮助我解决这个问题。谢谢!
哦,顺便说一下,这是 SOAPUI 上的 RAW 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">
<soapenv:Header>
<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">
<wsse:UsernameToken>
<wsse:Username>817221</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">1234</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<v1:CreateLeadRequest>
<createLead>
<v11:LeadSourceId>23627e70-a29e-e211-b8a8-005056b81ebe</v11:LeadSourceId>
<v11:AffiliateLeadReference>852800020</v11:AffiliateLeadReference>
<v11:Title>
<v12:Code>852800018</v12:Code>
</v11:Title>
<v11:Initials>MD</v11:Initials>
<v11:PreferredName>Marius</v11:PreferredName>
<v11:FirstName>Marius</v11:FirstName>
<v11:LastName>Drew</v11:LastName>
<v11:PreferredCorrespondenceLanguage>
<v13:Code>852800001</v13:Code>
<v13:Description>?</v13:Description>
</v11:PreferredCorrespondenceLanguage>
<v11:PreferredCommunicationMethod>
<v14:Code>852800000</v14:Code>
</v11:PreferredCommunicationMethod>
<v11:Campaign>
<v15:Code>95D9042A-440E-E311-A5EB-005056B81EA5</v15:Code>
</v11:Campaign>
<v11:HomePhoneNumber>0723621762</v11:HomePhoneNumber>
<v11:BusinessPhoneNumber>0723621762</v11:BusinessPhoneNumber>
<v11:MobilePhoneNumber>0723621762</v11:MobilePhoneNumber>
<v11:EmailAddress>mdrew@gmail.com</v11:EmailAddress>
<v11:Notes>IMU</v11:Notes>
<v11:ProductCategories>
<v16:Code>d000083d-229c-e211-b8a8-005056b81ebe</v16:Code>
</v11:ProductCategories>
</createLead>
</v1:CreateLeadRequest>
</soapenv:Body>
</soapenv:Envelope>
这在 SOAPUI 上完美运行,但它返回一个 soapenv:Client Internal Error on PHP。
这是我制作的另一个版本的脚本。不过,它仍然会发出内部错误。处理 Web 服务上的 WS-Security 的标头的构造可能存在问题。
<?php
$wsdl = "http://imupost.co.za/momentumcrm/CRMLeadServiceV10.wsdl";
$momurl = "https://integrationdev.momentum.co.za/sales/CRMService/CRMLeadService_v1_0/";
echo("Post to URL: $momurl\n");
$username = "derek";
$password = "Momentum1";
echo("<pre>\n");
$client = new SoapClient ($wsdl, array('location' => $momurl, 'style' => SOAP_DOCUMENT, 'trace' => true, 'soap_version' => SOAP_1_1, 'exceptions' => true, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP));
$header='
<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">
<wsse:UsernameToken wsu:Id="UsernameToken-45">
<wsse:Username>'.$username.'</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
';
echo("<pre>\n");
$headerSoapVar = new SoapVar($header,XSD_ANYXML);
$soapheader = new SoapHeader('wsse', "Security" , $headerSoapVar , true);
$client->__setSoapHeaders($soapheader);
$params['createLead'] = array(
'LeadSourceId' => '23627e70-a29e-e211-b8a8-005056b81ebe',
'AffiliateLeadReference' => '852800020',
'Title' => array('Code' => '852800018'),
'Initials' => 'MD',
'PreferredName' => 'Marius',
'FirstName' => 'Marius',
'LastName' => 'Drew',
'PreferredCorrespondenceLanguage' => array('Code' => '852800001'),
'PreferredCommunicationMethod' => array('Code' =>'852800000'),
'Campaign' => array('Code' => '95D9042A-440E-E311-A5EB-005056B81EA5'),
'HomePhoneNumber' => '0723621762',
'BusinessPhoneNumber' => '0723621762',
'MobilePhoneNumber' => '0723621762',
'EmailAddress' => 'mdrew@gmail.com',
'Notes' => 'IMU',
'ProductCategories' => array('Code' => 'd000083d-229c-e211-b8a8-005056b81ebe')
);
try
echo $result = $client->__call("createLead", array($params));
echo "REQUEST:\n" . htmlentities($client->__getLastRequest()) . "\n";
echo "REQUEST:\n" . $client->__getLastRequestHeaders() . "\n";
catch (Exception $e)
$ml = new SimpleXMLElement($client->__getLastRequest());
$ml->asXML('new.xml');
echo "REQUEST:\n" . htmlentities($client->__getLastRequest()) . "\n";
echo "REQUEST:\n" . $client->__getLastRequestHeaders() . "\n";
$msgs = $e->getMessage();
echo "Error: $msgs";
?>
有什么建议吗?我非常需要你的帮助。谢谢!
【问题讨论】:
PHP 的SoapClient
类绝对是要走的路。您能否为您尝试使用的服务添加/提供指向 WSDL 的链接?
嗨@GeorgeBrighton 他们给了我这个端点:integrationdev.momentum.co.za/sales/CRMService/…,他们还提供了 wsdl 和 xsd 文件。你可以在这里下载:sdrv.ms/16KC8o4
请为 libxml2 catalog.xml 提供一个目录文件,以使其独立于在线资源运行。
您为什么将 Soap 请求创建为字符串?我试图用你的重复问题重现(你现在删除了它),我得到一个错误,告诉 WSDL 有一个错误:致命错误:未捕获的 SoapFault 异常:" [WSDL] SOAP-ERROR: Parsing Schema: can 't import schema from 'imupost.co.za/xsd-src/Lead_1_0.xsd'" - 我建议您使用基于SoapClient
的示例更新您的问题,这样可以重现您遇到的问题。因为使用字符串请求对于 SOAP 来说很麻烦,所以您不会得到太多反馈。
我更新了问题并添加了另一个版本的脚本。
【参考方案1】:
使用SoapClient
,这应该让您了解如何创建 SOAP 调用:
try
$client = new SoapClient('CRMLeadServiceV10.wsdl');
$header = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', array(
'UsernameToken' => array(
'Username' => 817221,
'Password' => array(
'_' => 1234,
'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'))));
$params = new stdClass();
$params->createLead = new stdClass();
$params->createLead->LeadSourceId = 1234;
$result = $client->__soapCall('createLead', $params, null, $header);
print_r($result);
catch (SoapFault $e)
die($e->getMessage());
【讨论】:
谢谢乔治。我会试试这个。对于'Title'参数,我应该怎么写,因为它包含?
我收到此错误:“SOAP-ERROR: Encoding: object has no 'createLead' property”。我真的很想知道为什么。
SoapClient 应该负责为您编码。也许它正在寻找对象 - 请参阅我的编辑。
WSDL 中的soap:address 位置与makelsabido 在评论中放置的位置不同,更重要的是,WSDL 位置不存在。如果您使用此 WSDL 创建 SoapClient 代理,它将不起作用。另一方面,maikelsabido 评论中的位置具有传输级别的安全性和客户端证书身份验证(通过wireshark 确认),需要有效的 https 连接证书才能成功完成 https 肥皂调用。我不知道如何在 PHP SoapClient 中执行此操作...
@jlvaquero 那是他们提供给我的端点。 Web 服务使用 WS-Security。【参考方案2】:
我能够使用以下代码连接到网络服务:
$client = new SoapClient ($wsdl, array('location' => $momurl, 'action'=>$action, 'style' => SOAP_DOCUMENT, 'trace' => 1, 'soap_version' => SOAP_1_1, 'exceptions' => false, 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP, 'ssl_method' => SOAP_SSL_METHOD_TLS));
$header='<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">
<wsse:UsernameToken wsu:Id="UsernameToken-45">
<wsse:Username>'.$usname.'</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>';
$headerSoapVar = new SoapVar($header,XSD_ANYXML);
$soapheader = new SoapHeader('wsse', "Security" , $headerSoapVar , true);
$client->__setSoapHeaders($soapheader);
$params['/* the function or method that you want to use */'] = array(/* insert your parameters here */);
$result = $client->__soapCall("/* the function or method that you want to use */", $params);
检查您的请求的标题部分是否与我的相似。如果不是,那么只需使用您自己的标题。基本上,只需复制您请求的标头部分并设置用户名和密码的变量即可。我已经在另一个 web 服务上尝试过了,它工作正常。
【讨论】:
以上是关于Web 服务 SOAP 请求适用于 SOAPUI,但不适用于 PHP的主要内容,如果未能解决你的问题,请参考以下文章
在 SOAP UI 请求中将 cookie 作为请求标头发送以获取 REST Web 服务
对 SOAP Web 服务的 JQuery AJAX 调用 - 访问被拒绝。在 SOAP UI 中工作