如何使用cURL在php上发布XML文件? [英] How to POST an XML file using cURL on php?

查看:73
本文介绍了如何使用cURL在php上发布XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计算机上使用本地服务器,并且尝试制作2个php脚本来发送和接收xml文件.

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.

我要使用以下代码发送xml文件:

To send the xml file i use this code :

<?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;
?>

要接收xml文件,请使用以下代码:

To receive the xml file i use this code :

<?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

指的是行:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);

我不知道到底是什么.我接收到的xml文件已创建,但是当我打开它时,得到的是:

which i dont know what exactly does. The xml file i receive is created but when i open it i get this :

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

但是xml文件只有5kbs,所以这不是问题.

but the xml file is only 5kbs so this is not the problem.

有人知道我在这里应该做什么吗?我要做的就是制作一个脚本来发送xml文件,并编写一个脚本来接收它并将其另存为xml.

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.

推荐答案

curl_setopt($ch, CURLOPT_POSTFIELDS, $foo)设置请求的正文,即要发布的数据.它期望$foo是一组以数组形式提供的键/值对:

curl_setopt($ch, CURLOPT_POSTFIELDS, $foo) sets your request's body, the data to be posted. It expects $foo to be a set of key-value pairs provided either as an array:

$foo = array(
    'foo' => 'some value',
    'bar' => 2
);

或作为百分比编码的字符串:

or as a percent-encoded string:

$foo = 'foo=some%20value&bar=2'

相反,您提供的是$xml_builder变量,它是由simplexml_load_file($file)返回的SimpleXMLElement对象.

Instead, you're providing $xml_builder variable which is a SimpleXMLElement object returned by simplexml_load_file($file).

尝试一下:

$postfields = array(
    'xml' => $your_xml_as_string; // get it with file_get_contents() for example
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

然后在接收端:

$received_xml = $_POST['xml'];

这篇关于如何使用cURL在php上发布XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆