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

发布于 2024-11-03 13:54:42 字数 295 浏览 5 评论 0原文

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

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

还有其他 PHP 函数可以用来做这样的事情吗?我一直在四处寻找,但 PUT 请求信息很少。

谢谢。

I need to put a string of data like so: '< client>...<\client>' onto an XMl server (example url:'http://example.appspot.com/examples') using PHP.
(Context: Adding a new client's details to the server).

I have tried using CURLOPT_PUT, with a file and with just a string (since it requires CURLOPT_INFILESIZE and CURLOPT_INFILE) but it does not work!

Are there any other PHP functions that could be used to do such a thing? I have been looking around but PUT requests information is sparse.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒲公英的约定 2024-11-10 13:54:42
// 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);  
// 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);  
烟花易冷人易散 2024-11-10 13:54:42

因为到目前为止我还没有使用过 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 文档 # 原始数据请求

since I haven't worked with cURL so far I can't really answer to that topic. If you'd like to use cURL I'd suggest looking at the server log and see what actually didn't work (so: Was the output of the request really what it's supposed to be?)

If you don't mind switching over to another technology/library I'd suggest you to use the Zend HTTP Client which is really straight forward to use, simple to include and should satisfy all your needs. Especially as performing a PUT Request is as simple as that:

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

Code sample is from: Zend Framework Docs # RAW-Data Requests

月亮是我掰弯的 2024-11-10 13:54:42

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

我希望这会有所帮助!

Another way to add string body to the PUT request with CURL in PHP is:

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

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文