如何在 php 上使用 cURL 发布 XML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14309796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to POST an XML file using cURL on php?
提问by donparalias
I am using a local server on my computer and i am trying to make 2 php scripts to send an xml file and receive it.
我在我的计算机上使用本地服务器,我正在尝试制作 2 个 php 脚本来发送一个 xml 文件并接收它。
To send the xml file i use this code :
要发送 xml 文件,我使用以下代码:
<?php
/*
* XML Sender/Client.
*/
// Get our XML. You can declare it here or even load a file.
$file = 'http://localhost/iPM/books.xml';
if(!$xml_builder = simplexml_load_file($file))
exit('Failed to open '.$file);
// We send XML via CURL using POST with a http header of text/xml.
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://localhost/iPM/receiver.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, 'http://localhost/iPM/receiver.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
// Print CURL result.
echo $ch_result;
?>
To receive the xml file i use this code :
要接收 xml 文件,我使用以下代码:
<?php
/*
* XML Server.
*/
// We use php://input to get the raw $_POST results.
$xml_post = file_get_contents('php://input');
// If we receive data, save it.
if ($xml_post) {
$xml_file = 'received_xml_' . date('Y_m_d-H-i-s') . '.xml';
$fh = fopen($xml_file, 'w') or die();
fwrite($fh, $xml_post);
fclose($fh);
// Return, as we don't want to cause a loop by processing the code below.
return;
}
?>
When i run the post script i get this error :
当我运行发布脚本时,我收到此错误:
Notice: Array to string conversion in C:\xampp\htdocs\iPM\main.php on line 17
which refers to line :
指的是行:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
which i dont know what exactly does. The xml file i receive is created but when i open it i get this :
我不知道到底是做什么的。我收到的 xml 文件已创建,但是当我打开它时,我得到了这个:
XML Parsing Error: syntax error
Location: file:///C:/xampp/htdocs/iPM/received_xml_2013_01_14-01-06-09.xml
Line Number 1, Column 1:
I tried to comment this specific line as i thought the problem lies there but then when i run my post script i get this error :
我试图评论这个特定的行,因为我认为问题出在那里,但是当我运行我的帖子脚本时,我收到了这个错误:
Request entity too large!
The POST method does not allow the data transmitted, or the data volume exceeds the capacity limit.
If you think this is a server error, please contact the webmaster.
Error 413
but the xml file is only 5kbs so this is not the problem.
但 xml 文件只有 5kbs,所以这不是问题。
Does anyone have any idea what i should do here? All i am trying to do is make a script to send an xml file and a script to receive it and save it as an xml.
有谁知道我应该在这里做什么?我想要做的就是制作一个脚本来发送一个xml文件和一个脚本来接收它并将其保存为一个xml。
回答by lafor
curl_setopt($ch, CURLOPT_POSTFIELDS, $foo)sets your request's body, the data to be posted. It expects $footo be a set of key-value pairs provided either as an array:
curl_setopt($ch, CURLOPT_POSTFIELDS, $foo)设置您的请求正文,即要发布的数据。它期望$foo是一组作为数组提供的键值对:
$foo = array(
'foo' => 'some value',
'bar' => 2
);
or as a percent-encoded string:
或作为百分比编码的字符串:
$foo = 'foo=some%20value&bar=2'
Instead, you're providing $xml_buildervariable which is a SimpleXMLElementobject returned by simplexml_load_file($file).
相反,您提供的$xml_builder变量是SimpleXMLElement由simplexml_load_file($file).
Try this:
尝试这个:
$postfields = array(
'xml' => $your_xml_as_string; // get it with file_get_contents() for example
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
Then on the receiving end:
然后在接收端:
$received_xml = $_POST['xml'];

