在 PHP 中通过 HTTP PUT 发送文件
我已经努力了几个小时,试图找出如何完成这项工作。我正在尝试通过 HTTP-PUT 将文件发送到 eXist 数据库。服务器有用户身份验证,所以我试图做这样的事情:
我有文档要放置到的 URL 我有 eXist DB 的用户名和密码 我有需要通过 PUT 发送的内容
我尝试使用 cURL 但它会默默地失败 我尝试使用 PHP 流,但不断收到“错误 201/已创建”,但实际上没有创建文件。
任何有关这方面的帮助将不胜感激。
这是我尝试使用 PHP 流的一些示例代码
$data = file_get_contents($tmpFile); $header = array( "Authorization: Basic " . base64_encode($this->ci->config->item('ws_login') . ':' . $this->ci->config->item('ws_passwd')), "Content-Type: text/xml" ); $params = array( 'http' => array( 'method' => 'PUT', 'header' => $header, 'content' => $data)); $ctx = stream_context_create($params); $response = file_get_contents($url, false, $ctx);
I've been struggling for several hours trying to figure out how to get this work. I'm trying to send a file via HTTP-PUT to an eXist db. There is user authentication for the server, so I was trying to do something like this:
I have the URL where the doc is to be PUTted to
I have the username and password for the eXist DB
I have the content that needs to be sent via the PUT
I tried getting to work with cURL but it would fail silently
I tried to use PHP streams, but kept getting "error 201/created" but no file was actually created.
Any help with this would be GREATLY appreciated.
Here's some sample code I tried using PHP streams
$data = file_get_contents($tmpFile); $header = array( "Authorization: Basic " . base64_encode($this->ci->config->item('ws_login') . ':' . $this->ci->config->item('ws_passwd')), "Content-Type: text/xml" ); $params = array( 'http' => array( 'method' => 'PUT', 'header' => $header, 'content' => $data)); $ctx = stream_context_create($params); $response = file_get_contents($url, false, $ctx);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
啊哈!在与我桌上的脾气暴躁的矮人填充娃娃进行了一些“橡皮鸭”之后,我找到了解决方案:
Aha! After a little "rubber ducking" with the grumpy dwarf stuffed doll on my desk here, I figured out the solution:
CURL 对我有用。这是我的代码片段,
编辑:刚刚更新了我的代码。我自己不使用身份验证,因此没有进行测试。
CURL works for me. Here is snippet from my code,
EDIT: Just updated my code. I don't use authentication myself so this is not tested.
这对我有用......
许多标题行可能不是必需的......但当我与我的 couchdb 交谈时它会起作用,所以我还没有抽出时间来清除它们。
This works for me...
Many of the header lines are probably not necessary... but it works when I talk to my couchdb so I haven't gotten around to weeding them out.
如果您的 eXist-db 启用了 SOAP 接口,则有一个名为 PheXist 的开源库,它可以使与数据库的交互更加容易。
If your eXist-db has the SOAP interface enabled, there's an open-source library called PheXist that would make interacting with the database easier.
从
http ://www.littlehart.net/atthekeyboard/2008/01/11/how-to-http-put-a-file-somewhere-using-php/
from
http://www.littlehart.net/atthekeyboard/2008/01/11/how-to-http-put-a-file-somewhere-using-php/