在 PHP 中通过 HTTP PUT 发送文件

发布于 2024-08-10 13:02:37 字数 950 浏览 4 评论 0原文

我已经努力了几个小时,试图找出如何完成这项工作。我正在尝试通过 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 技术交流群。

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

发布评论

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

评论(5

鯉魚旗 2024-08-17 13:02:37

啊哈!在与我桌上的脾气暴躁的矮人填充娃娃进行了一些“橡皮鸭”之后,我找到了解决方案:

        $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 == '');

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 == '');
内心荒芜 2024-08-17 13:02:37

CURL 对我有用。这是我的代码片段,

                $handle = curl_init ($server_url);

                if ($handle)
                {
                    // specify custom header
                    $customHeader = array(
                        "Content-type: $file_type"
                    );
                    $curlOptArr = array(
                        CURLOPT_PUT => TRUE,
                        CURLOPT_HEADER => TRUE,
                        CURLOPT_HTTPHEADER => $customHeader,
                        CURLOPT_INFILESIZE => $file_size,
                        CURLOPT_INFILE => $file,
                        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                        CURLOPT_USERPWD => $user . ':' . $password,
                        CURLOPT_RETURNTRANSFER => TRUE
                    );
                    curl_setopt_array($handle, $curlOptArr);
                    $ret = curl_exec($handle);
                    $errRet = curl_error($handle);
                    curl_close($handle);

编辑:刚刚更新了我的代码。我自己不使用身份验证,因此没有进行测试。

CURL works for me. Here is snippet from my code,

                $handle = curl_init ($server_url);

                if ($handle)
                {
                    // specify custom header
                    $customHeader = array(
                        "Content-type: $file_type"
                    );
                    $curlOptArr = array(
                        CURLOPT_PUT => TRUE,
                        CURLOPT_HEADER => TRUE,
                        CURLOPT_HTTPHEADER => $customHeader,
                        CURLOPT_INFILESIZE => $file_size,
                        CURLOPT_INFILE => $file,
                        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                        CURLOPT_USERPWD => $user . ':' . $password,
                        CURLOPT_RETURNTRANSFER => TRUE
                    );
                    curl_setopt_array($handle, $curlOptArr);
                    $ret = curl_exec($handle);
                    $errRet = curl_error($handle);
                    curl_close($handle);

EDIT: Just updated my code. I don't use authentication myself so this is not tested.

水染的天色ゝ 2024-08-17 13:02:37

这对我有用......

function put($_server,$_file,$_data)
{
  $fp = @fsockopen ($_server, 80, $errno, $errstr, 30);
  if ($fp)
  {
    $p = "PUT $_file HTTP/1.0\r\n";
    $p.= "User-Agent: Mozilla/3.0 (Windows NT 5.0; U) Opera 7.21  [da]\r\n";
    $p.= "Host: $_server\r\n";
    $p.= "Accept: text/html, application/xml;q=0.9, application/xhtml+xml;q=0.9, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\r\n";
    $p.= "Accept-Language: da;q=1.0,en;q=0.9\r\n";
    $p.= "Accept-Charset: windows-1252, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1\r\n";
    $p.= "Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0\r\n";
    $p.= "Referer: http://www.nasa.gov/secret/flightplans.asp\r\n";
    $p.= "Content-type: application/x-www-form-urlencoded\r\n";
    $p.= "Content-length: ".strlen($_data)."\r\n";
    $p.= "\r\n";
    $p.= $_data;

    //echo($p);
    fputs ($fp, $p);
  }
  else die("dagnabbit : $errstr");

  while ($l=fgets($fp))
    echo($l);
  fclose($fp);
}

许多标题行可能不是必需的......但当我与我的 couchdb 交谈时它会起作用,所以我还没有抽出时间来清除它们。

This works for me...

function put($_server,$_file,$_data)
{
  $fp = @fsockopen ($_server, 80, $errno, $errstr, 30);
  if ($fp)
  {
    $p = "PUT $_file HTTP/1.0\r\n";
    $p.= "User-Agent: Mozilla/3.0 (Windows NT 5.0; U) Opera 7.21  [da]\r\n";
    $p.= "Host: $_server\r\n";
    $p.= "Accept: text/html, application/xml;q=0.9, application/xhtml+xml;q=0.9, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\r\n";
    $p.= "Accept-Language: da;q=1.0,en;q=0.9\r\n";
    $p.= "Accept-Charset: windows-1252, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1\r\n";
    $p.= "Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0\r\n";
    $p.= "Referer: http://www.nasa.gov/secret/flightplans.asp\r\n";
    $p.= "Content-type: application/x-www-form-urlencoded\r\n";
    $p.= "Content-length: ".strlen($_data)."\r\n";
    $p.= "\r\n";
    $p.= $_data;

    //echo($p);
    fputs ($fp, $p);
  }
  else die("dagnabbit : $errstr");

  while ($l=fgets($fp))
    echo($l);
  fclose($fp);
}

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.

蓝戈者 2024-08-17 13:02:37

如果您的 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.

情话墙 2024-08-17 13:02:37
function _publish($service, $doc) {
    $params = array(
        'http' => array(
            'method' => 'PUT'));
    $context = stream_context_create($params);
    $fp = fopen($service, 'rb', false, $context);
    $response = fwrite($fp,file_get_contents($doc));
    if ($response === false) {
        return false;
    }
    // Pull out the status code from the header
    $metaData = stream_get_meta_data($fp);
    preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $metaData['wrapper_data'][0], $matches);
    $code = end($matches[1]);
    if ($code == 200) {
        return true;
    } else {
        return false;
    }
}


http ://www.littlehart.net/atthekeyboard/2008/01/11/how-to-http-put-a-file-somewhere-using-php/

function _publish($service, $doc) {
    $params = array(
        'http' => array(
            'method' => 'PUT'));
    $context = stream_context_create($params);
    $fp = fopen($service, 'rb', false, $context);
    $response = fwrite($fp,file_get_contents($doc));
    if ($response === false) {
        return false;
    }
    // Pull out the status code from the header
    $metaData = stream_get_meta_data($fp);
    preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/", $metaData['wrapper_data'][0], $matches);
    $code = end($matches[1]);
    if ($code == 200) {
        return true;
    } else {
        return false;
    }
}

from
http://www.littlehart.net/atthekeyboard/2008/01/11/how-to-http-put-a-file-somewhere-using-php/

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