通过PHP中的HTTP PUT发送文件 [英] Sending a file via HTTP PUT in PHP

查看:158
本文介绍了通过PHP中的HTTP PUT发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力几个小时试图找出如何获得这项工作。我试图通过HTTP-PUT发送一个文件到eXist db。有服务器的用户身份验证,所以我试图做这样的事情:

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:

我有一个URL文档将被转移到
有eXist DB的用户名和密码
我有需要通过PUT发送的内容

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

我试图使用cURL,但它会失败默认
我试图使用PHP流,但是仍然得到错误201 /创建,但没有实际创建文件。

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.

以下是我使用PHP流程试用的示例代码

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);


推荐答案

Aha!在我的桌子上有一个橡皮鸭子和脾气暴躁的矮人填充娃娃,我想出了解决方案:

Aha! After a little "rubber ducking" with the grumpy dwarf stuffed doll on my desk here, I figured out the solution:


        $data = file_get_contents($tmpFile);
         $params = array(
             'http' => array(
                 'method' => 'PUT',
                 'header' => "Authorization: Basic " . base64_encode($this->ci->config->item('ws_login') . ':' . $this->ci->config->item('ws_passwd')) . "\r\nContent-type: text/xml\r\n",
                 'content' => file_get_contents($tmpFile)
             )
         );
         $ctx = stream_context_create($params);
         $response = @file_get_contents($url, false, $ctx);

         return ($response == '');

这篇关于通过PHP中的HTTP PUT发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆