php - 在 PHP 中通过 HTTP PUT 发送文件

标签 php curl stream http-put

我已经苦苦挣扎了几个小时,试图弄清楚如何完成这项工作。我正在尝试通过 HTTP-PUT 将文件发送到 eXist 数据库。服务器有用户身份验证,所以我试图做这样的事情:

我有文档要放置到的 URL 我有 eXist 数据库的用户名和密码 我有需要通过 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);

最佳答案

啊哈!在与我 table 上脾气暴躁的矮人毛绒玩偶进行了一次“橡皮鸭”游戏之后,我找到了解决方案:

        $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 - 在 PHP 中通过 HTTP PUT 发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1691530/

相关文章:

javascript - 隐藏的同名输入 : how to send only the values of selected checkboxes?

Php DateTime 实例使用时间戳返回错误结果

javascript - Node.js curl : (55) Send failure: Broken pipe error

curl - cron 作业的最大长度

logging - 实时日志处理和警报生成

python - 如何使用Python流对象?

php - 如何链接 Laravel 模块内部的 Assets ?

php - 我正在尝试使用 mysqli 查询我的 mysql 数据库,但是出现内存错误

file-upload - 帮助 AppleScript cURL 文件上传

scala - 如何使用 FS2 中的分类器函数对对象进行分组?