简体   繁体   English

PHP SOAP XML根本不起作用

[英]PHP SOAP XML not working at all

I need to create a SOAP XML schema to connect PHP to SOAP Server . 我需要创建一个SOAP XML模式以将PHP连接到SOAP Server I read about SOAPClient class in PHP docs but it's quite unclear how to actually create an XML Schema like the one I need. 我在PHP文档中读到有关SOAPClient类的信息,但还不清楚如何像我需要的那样实际创建XML Schema I've also read about SOAPVar but I'm not able to make it work, probably because it's the first time that I use PHP SOAP . 我还阅读了有关SOAPVar但我无法使其正常运行,可能是因为这是我第一次使用PHP SOAP
The function I'm calling from the server is named insertContact which requires a string password and a Contact object, where source and emailAddress1 are the only mandatory fields. 我从服务器调用的函数名为insertContact ,它需要一个字符串password和一个Contact对象,其中sourceemailAddress1是唯一的必填字段。

Anyway, this should be the XML schema: 无论如何,这应该是XML模式:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
      <ns9:HeaderInfo xmlns:ns9="http://example.com/" xmlns:ns8="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:ns7="http://tempuri.org/" xmlns:ns6="http://schemas.datacontract.org/2004/07/FileServiceWcfServiceLibrary.Domain" xmlns:ns5="http://example.com.Utilities" xmlns:ns4="http://example.com.Entity" xmlns:ns3="http://example.com" xmlns:ns2="http://example.com.PrimaryKey">
         <infos>
            <entries>
               <key>apiKey</key>
               <value>key</value>
            </entries>
            <entries>
               <key>apiSecret</key>
               <value>secret</value>
            </entries>
         </infos>
         <type>source</type>
      </ns9:HeaderInfo>
   </soap:Header>
   <soap:Body>
      <ns1:insertContact xmlns:ns1="http://example.com/">
         <password xmlns:ns2="http://example.com.PrimaryKey" xmlns:ns3="http://example.com" xmlns:ns4="http://example.com.Entity" xmlns:ns5="http://example.com.Utilities" xmlns:ns6="http://schemas.datacontract.org/2004/07/FileServiceWcfServiceLibrary.Domain" xmlns:ns7="http://tempuri.org/" xmlns:ns8="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:ns9="http://example.com/">password</password>
         <contact xmlns:ns2="http://example.com.PrimaryKey" xmlns:ns3="http://example.com" xmlns:ns4="http://example.com.Entity" xmlns:ns5="http://example.com.Utilities" xmlns:ns6="http://schemas.datacontract.org/2004/07/FileServiceWcfServiceLibrary.Domain" xmlns:ns7="http://tempuri.org/" xmlns:ns8="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:ns9="http://example.com/">
            <ns3:Address1_City xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Address1_Line1 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Address1_PostalCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Address1_StateOrProvince xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Address1_Telephone1 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Birthdate xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Country>
               <ns2:CountryCode>GB</ns2:CountryCode>
            </ns3:Country>
            <ns3:CreatedBy xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:CreatedOn xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:EmailAddress1>email@example.com</ns3:EmailAddress1>
            <ns3:FacebookId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:FirstName>FirstName</ns3:FirstName>
            <ns3:FiscalCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:GooglePlusId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Id xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:IdBadgeminton xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:isM xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:isMotoM xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:IsPir xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:IsCar xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:isF1 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:isMoto></ns3:isMoto>
            <ns3:isTyre xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:LangCode>en</ns3:LangCode>
            <ns3:LastName>LastName</ns3:LastName>
            <ns3:MobilePhone xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:ModifiedBy xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:ModifiedOn xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:NickName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:Privacy>true</ns3:Privacy>
            <ns3:PrivacyGroup>false</ns3:PrivacyGroup>
            <ns3:PrivacyThird>false</ns3:PrivacyThird>
            <ns3:Source>SoapUI</ns3:Source>
            <ns3:StateCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:StatusCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:SubscriptionConfirmed xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns3:TwitterId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
         </contact>
      </ns1:insertContact>
   </soap:Body>
</soap:Envelope>

I have no idea on how to do this schema, I've just understood how to create Headers, but nothing more at all. 我不知道如何执行此架构,我仅了解如何创建标头,但仅此而已。 This I what I've tried so far: 这是我到目前为止尝试过的:

$soapClient=new SoapClient(WSDL_URI,array(
        'trace' => true,
        'login' => 'user',
        'password' => 'password'
    ));
    $soapHeader=new SOAPHeader(API_NAMESPACE,'HeaderInfo',array(
        'infos' => array(
            'entries' => array(
                'key' => 'apiKey',
                'value' => APIKEY
            ),
            'entries' => array(
                'key' => 'apiSecret',
                'value' => APISECRET
            )
        ),
        'type' => 'source'
    ));
    $soapClient->__setSoapHeaders($soapHeader);
    $soapContactParams=[];
    $soapContactParams[]=new SoapVar('email@example.com',XSD_STRING,null,null,'EmailAddress1');
    $soapContactParams[]=new SoapVar('source',XSD_STRING,null,null,'Source');
    $soapContact=new SoapVar($soapContactParams,SOAP_ENC_OBJECT,null,null,'contact');
    $soapClient->insertContact(password,$soapContact);

Which is giving me this error: 这给了我这个错误:

Fatal error: Uncaught SoapFault exception: [soap:Server] Fault occurred while processing. in /var/www/example.com/example.com/test/index.php:34 Stack trace: #0 /var/www/example.com/example.com/test/index.php(34): SoapClient->__call('insertContact', Array) #1 /var/www/example.com/example.com/test/index.php(34): SoapClient->insertContact('password', Object(SoapVar)) #2 {main} thrown in /var/www/example.com/example.com/test/index.php on line 34

I'm actually quite new to PHP SOAP , and let me say it, I don't like it at all. 实际上,我对PHP SOAP还是很PHP SOAP ,可以这么说,我一点都不喜欢。 I don't even know if I'm building a well-formatted XML because I can't print it. 我什至不知道是否要构建格式正确的XML,因为我无法打印它。

Last Edit 最后编辑

I've canceled all the edits I've done before because they were all pointing to the same direction: nothing works at all. 我已经取消了之前所做的所有编辑,因为它们都指向同一方向:根本没有任何作用。

So I've tried copying their example XML , but it doens't work. 因此,我尝试复制他们的示例XML ,但是它不起作用。 I think I will stop trying understanding SOAP , at least for PHP , because it's quite annoying, boring, and not well documented at all. 我认为我将停止尝试至少对于PHP理解SOAP ,因为它很烦人,无聊且没有完整的文档。 ( why do I need to hardcode XML? ) (为什么我需要对XML进行硬编码?)

After 3 weeks of intense work, I've found the problem. 经过3周的紧张工作,我发现了问题所在。

I'm using WSDL, remember it, WSDL has been a double-edged weapon for me. 我正在使用WSDL,请记住,WSDL对我来说是双刃武器。 As read in the docs, using it will let SoapClient automatically find the endpoint, all the functions and all the types which SoapServer requires, and that's good. 正如在文档中所读到的,使用它可以使SoapClient自动查找端点, SoapServer所需的所有功能和所有类型,这很好。

My problem was the endpoint, and I really,really suggest you to double check that, because mine wasn't right. 我的问题是端点,真的,我真的建议您仔细检查一下,因为我的说法不对。 I had to force it because I was getting only unable to connect to host errors, and nothing more. 我不得不强迫它,因为我只能unable to connect to host错误,仅此而已。

So this is my code that made it work. 所以这是我的代码,使其起作用。 Finally 最后

class SoapClientDebug extends SoapClient {
    function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
        $this->server = new SoapServer($wsdl, $options);
    }
    public function __doRequest($request, $location, $action, $version, $one_way = 0) {
        $location=THE_CORRECT_ENDPOINT;
        return parent::__doRequest($request, $location, $action, $version, $one_way = 0);
    }
}

Trust me, check it. 相信我,检查一下。 I've lost 3 weeks of work by searching for a problem, when the problem was actually simple. 当问题实际上很简单时,我通过寻找问题已经失去了3个星期的工作。

Anyway, I still hate SOAP on PHP :) 无论如何,我仍然讨厌PHP上的SOAP :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM