php - Web 服务 SOAP 请求适用于 SOAPUI 但不适用于 PHP

标签 php xml web-services soap wsdl

我已经尝试了几乎所有我能找到的可能的解决方案,但我似乎无法让这个脚本正常工作。以下是我之前的一些尝试:

How to use wse-php library to connect to secured webservices via SOAP

https://stackoverflow.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 东西很痛苦。我尝试使用 StackOverflow 上的建议,例如创建一个扩展到 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 上完美运行,但在 PHP 上返回 soapenv:Client Internal Error。

这是我制作的脚本的另一个版本。它仍然给出了一个内部错误。可能处理 Web 服务上的 WS-Security 的 header 构造有问题。

<?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"; 
} 
?>

有什么建议吗?我非常需要你的帮助。谢谢!

最佳答案

使用 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());
}

关于php - Web 服务 SOAP 请求适用于 SOAPUI 但不适用于 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995643/

相关文章:

php - 在 PHP 中,我可以区分 intval(null) 和 intval ("0") 的结果吗?

php - WAMP 中的 SQL Server 而不是 MYSQL

java - xml:元素的值无效

android - 如何在 Material 按钮上写多行文本?

xml - xpath 日期比较

java - clientApplicationContext xml 文件出现问题

php - 数据库连接如何在php-mysql中工作

php - 具有多个计数的选项卡导航栏

java - 在 Web 服务中调用 getUserPrincipal() 返回匿名

javascript - 具有多个客户端平台的应用程序服务器 API 的 session 设计