1

私は自分のコンピューターでローカル サーバーを使用しており、xml ファイルを送信して受信する 2 つの php スクリプトを作成しようとしています。

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

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

ポスト スクリプトを実行すると、次のエラーが発生します。

Notice: Array to string conversion in C:\xampp\htdocs\iPM\main.php on line 17

行を参照します:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);

私は正確に何をしているのか分かりません。私が受け取った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:

問題がそこにあると思ったので、この特定の行にコメントしようとしましたが、ポストスクリプトを実行すると、このエラーが発生します:

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 しかないので、これは問題ではありません。

私がここで何をすべきか誰かが知っていますか? 私がやろうとしているのは、xmlファイルを送信するスクリプトと、それを受信して​​xmlとして保存するスクリプトを作成することだけです。

4

1 に答える 1

6

curl_setopt($ch, CURLOPT_POSTFIELDS, $foo)リクエストの本文、投稿するデータを設定します。$fooこれは、配列として提供されるキーと値のペアのセットであることが期待されます。

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

またはパーセントエンコードされた文字列として:

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

代わりに、によって返されるオブジェクト$xml_builderである変数を提供しています。SimpleXMLElementsimplexml_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'];
于 2013-01-14T01:09:28.543 に答える