我使用cURL在同一网站上将一些XML从一个页面发布到另一个页面:
我从数组生成XML(这个位工作正常):
$xml = new SimpleXMLElement('<test/>');
array_walk_recursive($arr, array ($xml, 'addChild'));
$xmlxml = $xml->asXML()
然后使用cURL发布到另一页:
$url = "http://www.test.com/feedtest.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlxml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
这会产生错误:
警告:simplexml_load_file()[function.simplexml-load-file]:I / O警告:无法加载外部实体“&lt;?xml version =”1.0“?&gt;&lt; test&gt; [.... ]“在”xxxxx / feedtest.php第16行“
我做错了什么?
我应该补充一下这是在另一页上发生的事情:
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
$postFile = file_get_contents('php://input');
}
$xml = simplexml_load_file($postFile);
最后一行是触发错误的行。
答案 0 :(得分:1)
这是一个非常微不足道的错误,容易错过:
$postFile = file_get_contents('php://input');
^^^^^^^^^^^^^^^^^
这会将XML字符串放入$postFile
。但是你将它用作文件名:
$xml = simplexml_load_file($postFile);
^^^^
相反,你可以这样加载它:
$postFile = 'php://input';
这是一个完美的有效文件名。所以稍后的代码应该可以工作:
$xml = simplexml_load_file($postFile);
或者你可以加载字符串:
$postFile = file_get_contents('php://input');
...
$xml = simplexml_load_string($postFile);