PHP - SoapClient()返回致命错误:未捕获的SoapFault异常:[xml_structure]字符串无法解析为XML

时间:2018-02-21 18:36:56

标签: php xml soap soap-client

我已经有了这个代码来向网络服务发出请求,但是它会在通话时发送Fatal error: Uncaught SoapFault exception: [xml_structure] String could not be parsed as XML

任何想法我怎么解决它?

所有以$为前缀的变量设置得很好,问题似乎与我尝试调用该方法有关。

完整代码:

$client = new SoapClient("url/to/wsdl");

$contract = array(
    'product' => array(
        'prefix' => 'MOP',
    ),
    'participants' => array(
        'customers' => array(
            'main_borrower' => array(
                'personal_data' => array(
                    'pesel' => $pesel,
                    'firstname' => $firstname,
                    'lastname' => $lastname,
                    'sex' => $xmlsex,
                ),
                'contact_data' => array(
                    'addresses' => array(
                        'address' => array(
                            'type' => 'registered',
                            'street_name' => $city,
                            'block_number' => '1',
                            'flat_number' => '1',
                            'postal_code' => $postcode,
                            'city' => $city,
                        ),
                    ),
                    'phones_mobile' => array(
                        'phone_mobile' => array(
                            'type' => 'personal',
                            'number' => $phone,
                        ),
                    ),
                ),
                'incomes' => array(
                    'income' => array(
                        'type' => 'employment',
                        'main_income' => 'true',
                        'fixed_term_contract' => 'false',
                        'paychecks' => array(
                            'paycheck' => array(
                                'amount_net' => array(
                                    'amount' => $price,
                                    'currency' => 'PLN',
                                ),
                                'type' => 'base',
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
);

$parameters = array(
'user_login' => 'xxx',
'user_password' => 'xxx',
'contract' => $contract,
);
$response = $client->newApplication($parameters);

此外 - 之前我使用过对象(=> ['xxx'=>'xxx'],)而不是数组,但仍然不会这样做。

1 个答案:

答案 0 :(得分:0)

实际上我犯的错误并不是$contractXML对象。

为此,我使用了以下示例:

//function defination to convert array to xml
function array_to_xml($array, &$xml_user_info) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_user_info->addChild("$key");
                array_to_xml($value, $subnode);
            }else{
                $subnode = $xml_user_info->addChild("item$key");
                array_to_xml($value, $subnode);
            }
        }else {
            $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><user_info></user_info>");

//function call to convert array to xml
array_to_xml($users_array,$xml_user_info);

//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xml');

//success and error message based on xml creation
if($xml_file){
    echo 'XML file have been generated successfully.';
}else{
    echo 'XML file generation error.';
}

原创版本并向作者致敬:https://www.codexworld.com/convert-array-to-xml-in-php/