繁体   English   中英

错误:在PHP中调用SOAP cilent时“对象引用未设置为对象的实例”

[英]Error : “Object reference not set to an instance of an object” when calling SOAP cilent in PHP

这是来自wsdl的数据

<s:element name="CreateDraftLead">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="formId" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="FirstName" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="LastName" type="s:string"/>
<s:element minOccurs="1" maxOccurs="1" name="Gender" nillable="true" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="AgeGroup" nillable="true" type="s:int"/>
<s:element minOccurs="0" maxOccurs="1" name="MobilePhone" type="s:string"/>
<s:element minOccurs="1" maxOccurs="1" name="Birthday" nillable="true" type="s:dateTime"/>
<s:element minOccurs="0" maxOccurs="1" name="District" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="email" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Region" type="s:string"/>
<s:element minOccurs="1" maxOccurs="1" name="LocationId" nillable="true" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="LeadSourceId" nillable="true" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="CompanyId" nillable="true" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="SourceCampaignId" nillable="true" type="s:int"/>
<s:element minOccurs="1" maxOccurs="1" name="AllowEmail" nillable="true" type="s:boolean"/>
<s:element minOccurs="0" maxOccurs="1" name="Questions" type="tns:ArrayOfQuestion"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfQuestion">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Question" nillable="true" type="tns:Question"/>
</s:sequence>
</s:complexType>
<s:complexType name="Question">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Key" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="Value" type="s:string"/>
</s:sequence>
</s:complexType>

这是我的尝试代码

$client = new SoapClient("http://demo/demo.asmx?wsdl", array('trace' => 1, "exception" => 0));


        $data = array(
            'formId' => 1,
            'LocationId' => 1,
            'FirstName' => 'test',
            'LastName' => 'test',
            'MobilePhone' => 11111111,
            'email' => 'test@test.com',
            'AllowEmail' => 0,
            'Region' => 'HK',
            'LeadSourceId' => 11,
            'Questions' => array(
                "Question" => array("AgeGroup" => 10),
                "Question" => array("Gender" => 1),
                "Question" => array("Remarks" => 'test'),
                "Question" => array("Are you currently a member of any yoga or fitness centre?  " => 'Yes'),
            )
        );



        $result = $client->CreateDraftLead(array('parameters' => $data));

        var_dump($result);

这是由输入数据的结构引起的,如何解决? 谢谢你的帮助。

错误信息:

Fatal error: Uncaught SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object. in C:\xampp\htdocs\test_soap.php:60 Stack trace: #0 C:\xampp\htdocs\test_soap.php(60): SoapClient->__call('CreateDraftLead', Array) #1 C:\xampp\htdocs\test_soap.php(60): SoapClient->CreateDraftLead(Array) #2 {main} thrown in C:\xampp\htdocs\test_soap.php on line 60

更新:

谢谢你的帮助。 现在可以发送到服务器

<?php
$client = new SoapClient("http://demo/demo.asmx?wsdl");

$data = array(
    'formId' => '1',
    'FirstName' => 'test',
    'LastName' => 'test',
    //'Gender' => 1,
    //'AgeGroup' => 10,
    'MobilePhone' => '11111111',
    'Birthday' => null,
    'District' => 'HK',
    'email' => 'test@test.com',
    'Region' => 'HK',
    'LocationId' => 1,
    'LeadSourceId' => 11,
    'CompanyId' => null,
    'SourceCampaignId' => null,
    'AllowEmail' => false,
    'Questions' => array(
        "Question" => array("AgeGroup" => 10),
        "Question" => array("Gender" => 1),
        "Question" => array("Remarks" => 'test'),
        "Question" => array("Are you currently a member of any yoga or fitness centre?  " => 'Yes'),
    )
);

$result = $client->__soapCall("CreateDraftLead", array($data));

var_dump($result);
?>

问题仍然存在:

1)AllowEmail接受布尔值,但是0,1,true,false返回错误:无效的列名'false'/来自服务器的列名'true'无效,但它不是例外

2)是minOccurs ='1'表示必须提供? 经过测试可以忽略该字段,服务器仍然可以返回成功

谢谢你的努力。

请尝试以下(未测试):

<?php
$client = new SoapClient("http://demo/demo.asmx?wsdl", array('trace' => 1, "exception" => 0));

$data = array(
    'formId' => 1,
    'LocationId' => 1,
    'FirstName' => 'test',
    'LastName' => 'test',
    'MobilePhone' => 11111111,
    'email' => 'test@test.com',
    'AllowEmail' => 0,
    'Region' => 'HK',
    'LeadSourceId' => 11,
    'Questions' => array(
        "Question" => array("AgeGroup" => 10),
        "Question" => array("Gender" => 1),
        "Question" => array("Remarks" => 'test'),
        "Question" => array("Are you currently a member of any yoga or fitness centre?  " => 'Yes'),
    )
);

$result = $client->__soapCall("CreateDraftLead", array($data));

var_dump($result);

此外,你似乎没有提供像Gender等所有必要的信息..你应该检查这个。

你可以使用“0”或intval(false)

<?php
  $client = new SoapClient("http://demo/demo.asmx?wsdl", array('trace' => 1, "exception" => 0));

 $data = array(
   'formId' => 1,
   'LocationId' => 1,
   'FirstName' => 'test',
   'LastName' => 'test',
   'MobilePhone' => 11111111,
   'email' => 'test@test.com',
   'AllowEmail' => "0",
   'Region' => 'HK',
   'LeadSourceId' => 11,
   'Questions' => array(
       "Question" => array("AgeGroup" => 10),
       "Question" => array("Gender" => 1),
       "Question" => array("Remarks" => 'test'),
       "Question" => array("Are you currently a member of any yoga or fitness centre?  " => 'Yes'),
   )
 );

$result = $client->__soapCall("CreateDraftLead", array($data));

var_dump($result);

要么

<?php
$client = new SoapClient("http://demo/demo.asmx?wsdl", array('trace' => 1, "exception" => 0));

$data = array(
   'formId' => 1,
   'LocationId' => 1,
   'FirstName' => 'test',
   'LastName' => 'test',
   'MobilePhone' => 11111111,
   'email' => 'test@test.com',
   'AllowEmail' => intval(false),
   'Region' => 'HK',
   'LeadSourceId' => 11,
   'Questions' => array(
       "Question" => array("AgeGroup" => 10),
       "Question" => array("Gender" => 1),
       "Question" => array("Remarks" => 'test'),
       "Question" => array("Are you currently a member of any yoga or fitness centre?  " => 'Yes'),
   )
);

$result = $client->__soapCall("CreateDraftLead", array($data));

var_dump($result);

我将测试另一个技巧,为您的服务发送布尔值:

// most probably a solution
'AllowEmail' => new \SoapVar('true', XSD_STRING, 's:boolean')
// or less probably solution, but still possible I guess
'AllowEmail' => new \SoapVar('true', XSD_BOOLEAN, 's:boolean')

我假设这是这样做的,因为当你试图发送0|1|true|false它返回一个没有这样的COLUMN的奇怪错误。

2)是minOccurs ='1'表示必须提供? 经过测试可以忽略该字段,服务器仍然可以返回成功

大概吧。 但它仍然在另一方面实施。 我曾经将一个API与WSDL极端集成在一起,远非你能用它做什么。

暂无
暂无

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

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