我正在尝试从PHP脚本向XML中心发送和接收XML数据,然后将其转换为PHP数组。
首先,这是我的PHP脚本:
if( ! $xml = simplexml_load_file('webtransfer.xml') )
{
data['status'] = "Failed to load XML file.";
echo json_encode($data);
}
else
{
// changing some data
$xml->product= $product;
$xml->user= $user;
$xml->time = $time;
$xml->msisdn = $msisdn ;
$xml->asXML('webtransfer.xml');
$xml_post_string = $xml->asXML();
// send data to center
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/xmlcenter/');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// return XML
$xmlstring= curl_exec($ch);
curl_close($ch);
如您所知,到目前为止代码还可以。但是当我尝试将从XML中心收到的XML数据转换为PHP数组时,我无法使用此代码执行此操作:
$array = simplexml_load_string($xmlstring);
$result= $array->result;
$msg= $array->msg;
$data['result'] = $result;
$data['msg'] = $msg;
$data['status'] = "OK";
echo json_encode($data);
我希望有人可以帮助我。另外,这是XML中心的回报:
<?xml version="1.0"?>
<etransaction>
<result>SUCCESS</result>
<msg>Success message ...</msg>
<trxid>123456789</trxid>
</etransaction>
感谢。