PHP,读取通过cURL发送的XML文件 [英] PHP , read XML file sent via cURL
问题描述
我有这段代码,我通过cURL将XML文件中的数据发送到新闻办公室.现在,我希望得到媒体的反馈,以确认或确认我的订单.我也希望将其保存在XML文件中.我知道如何通过curl发送文件,现在我想知道如何接收文件,以便可以读出数据.欢迎任何建议.
I have this code where I send data in an XML file via cURL to a press office. Now I want a feedback from the press that my orders are confirmed or done. I would like to have that in an XML file as well. I know how I send file via curl, now I would like to know how do i receive them so i can read out the data. Any suggestions are welcome.
这是我发送XML的方式:
this is how i send my XML:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $incomm_prod_server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, str_replace('{voucher_code}', $voucher_code, $xml_data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
这就是我在获取XML方面所做的事情:
So this is what i do on the ither side to get the XML:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$result_xml = simplexml_load_string(curl_exec($ch));
但是我得到的结果是bool(false),所以没有发送xml吗?
But i get bool(false) as result back, so there is no xml sent?
我可以这样访问数据:
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
$postText = file_get_contents('php://input');
}
die(var_dump($postText));
我最后一次编辑,也许会对其他人有帮助,我现在以这种方式访问我的xml:
I edit one last time, maybe it will help others, i access now my xml this way:
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
$postText = file_get_contents('php://input');
}
$xml = new SimpleXMLElement($postText);
$packing_number = $xml->xpath('/feedback/packing_number');
$packing_status = $xml->xpath('/feedback/packing_status');
这将为您提供一个数组,您可以像以下方式访问它:
this will give you an array back, you can access it like:
$ packing_number [0]
$packing_number[0]
或者只是循环通过它.
推荐答案
好,因此您上面发布的代码不会真的发送XML文件.它所做的只是将XML文件的内容放入附加到请求的$_POST
变量中.
Ok so the code you posted above doesn't really send the XML file. All it does is place the content of that XML file into a $_POST
variable attached to the request.
要接收数据(在另一端),您要做的就是查看$_POST
变量,您的XML数据应该在该位置.您将设置一个脚本,然后将数据发布到脚本(可能使用与上面相同的方法),然后您便可以访问该内容.
To receive data (on the other side), all you have to do is take a look into the $_POST
variable and your XML data should be there. You'd setup a script and data would be posted to it (possibly using the same method you are using above), and the content will be accessible to you.
这篇关于PHP,读取通过cURL发送的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!