使用PHP将数据字符串输出到XML服务器

时间:2011-04-28 14:31:21

标签: php xml curl put

我需要输入一串数据,如下所示:'<客户机GT; ...< \客户机GT;”使用PHP在XMl服务器上(例如url:'http://example.appspot.com/examples')。 (上下文:将新客户端的详细信息添加到服务器)。

我尝试过使用CURLOPT_PUT,只有一个文件和一个字符串(因为它需要CURLOPT_INFILESIZE和CURLOPT_INFILE),但它不起作用!

是否还有其他PHP函数可用于执行此类操作?我一直在环顾四周,但PUT请求的信息很少。

感谢。

3 个答案:

答案 0 :(得分: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);  

答案 1 :(得分: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

答案 2 :(得分: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
  ?>

我希望这有帮助!