2011-04-28 24 views
1

我需要把一個像這樣的數據字符串:'<客戶端> ... < \客戶端>'到XMl服務器(示例url:http://示例。 appspot.com/examples')使用PHP。 (上下文:將新客戶端的詳細信息添加到服務器)。PUT字符串的數據到XML服務器使用PHP

我試過使用CURLOPT_PUT,一個文件和一個字符串(因爲它需要CURLOPT_INFILESIZE和CURLOPT_INFILE),但它不工作!

是否有任何其他PHP函數可以用來做這樣的事情?我一直在環顧四周,但PUT請求信息稀少。

謝謝。

回答

0

因爲我到目前爲止還沒有使用cURL,所以我無法真正回答該主題。如果你想使用cURL我建議看看服務器日誌,看看實際上沒有工作(所以:請求的輸出真的是它應該是什麼?)

如果你不'不介意切換到另一個技術/庫我建議你使用Zend HTTP Client這是非常簡單易用,簡單包括,並應滿足您的所有需求。特別是作爲執行PUT請求是如此簡單:

<?php 
    // of course, perform require('Zend/...') and 
    // $client = new Zend_HTTP_Client() stuff before 
    // ... 
    [...] 
    $xml = '<yourxmlstuffhere>.....</...>'; 
    $client->setRawData($xml)->setEncType('text/xml')->request('PUT'); 
?> 

代碼樣品來自:Zend Framework Docs # RAW-Data Requests

1
// Start curl 
    $ch = curl_init(); 
// URL for curl 
    $url = "http://example.appspot.com/examples"; 

// Put string into a temporary file 
    $putString = '<client>the RAW data string I want to send</client>'; 

/** use a max of 256KB of RAM before going to disk */ 
    $putData = fopen('php://temp/maxmemory:256000', 'w'); 
    if (!$putData) { 
     die('could not open temp memory data'); 
    } 
fwrite($putData, $putString); 
fseek($putData, 0); 

// Headers 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
// Binary transfer i.e. --data-BINARY 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, $url); 
// Using a PUT method i.e. -XPUT 
curl_setopt($ch, CURLOPT_PUT, true); 
// Instead of POST fields use these settings 
curl_setopt($ch, CURLOPT_INFILE, $putData); 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString)); 

$output = curl_exec($ch); 
echo $output; 

// Close the file 
fclose($putData); 
// Stop curl 
curl_close($ch); 
0

另一種方法串體添加到與在PHP CURL PUT請求是:

<?php 
     $data = 'My string'; 
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // Define method type 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Set data to the body request 
    ?> 

我希望這有助於!

相關問題