2013-01-14 24 views
1

我在我的計算機上使用本地服務器,我試圖讓2個PHP腳本發送一個xml文件並接收它。如何使用cURL在PHP上發佈XML文件?

要發送的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。

回答

6

curl_setopt($ch, CURLOPT_POSTFIELDS, $foo)設置您的請求的正文,要發佈的數據。它期望$foo是一組提供是作爲數組鍵值對:

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

或作爲百分比編碼串:

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

相反,您要提供$xml_builder變量這是由simplexml_load_file($file)返回的SimpleXMLElement對象。在接收端

$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']; 
+0

我想你說的話,但我得到一個錯誤:在此行找到多個註釋: \t - 語法錯誤,意外'EOF', \t期待')' \t - 語法錯誤,意外的'=', \t期待')' \t - 1改線 。你確定synta是正確的嗎?你在接收方面意味着什麼?究竟在哪裏?在receiver.php文件的最後?我沒有看到有意義!? – donparalias

+0

我只是這樣修復它:我用這樣的file_get_contents:$ xml = file_get_contents(「books.xml」);然後我只是把:curl_setopt($ ch,CURLOPT_POSTFIELDS,$ xml);它的工作! – donparalias

+0

使用SimpleXMLElement,您需要執行$ xml_builder-> asXml(); – user2414000