wp rest api json到xml文件上的PHP错误

wp rest api json到xml文件上的PHP错误,api,rest,Api,Rest,大家好,我刚开始学习PHP,并在一个使用wp rest api将json数组转换为xml文件的小项目中工作。在尝试将数组转换为xml文件时,我遇到了以下错误,我不知道是什么导致了以下错误 致命错误:在第15行的/home/eggshell/spareegg.net/wprest_test/wprest_phptest-xml.php中对非对象调用成员函数addChild() <?php //function defination to convert array to xml funct

大家好,我刚开始学习PHP,并在一个使用wp rest api将json数组转换为xml文件的小项目中工作。在尝试将数组转换为xml文件时,我遇到了以下错误,我不知道是什么导致了以下错误

致命错误:在第15行的/home/eggshell/spareegg.net/wprest_test/wprest_phptest-xml.php中对非对象调用成员函数addChild()

<?php

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

$options  = array (
 'http' => 
  array (
'ignore_errors' => true,
 ),
);

$context  = stream_context_create( $options );
$response = file_get_contents(
'http://www.administration.spareegg.net/wp-json/wp/v2/posts',
false,
$context
);

$response = json_decode( $response );

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

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

//saving generated xml file
$xml_file = $xml_user_info->asXML('test.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.';
}

?>