简体   繁体   English

在PHP中通过HTTP PUT发送文件

[英]Sending a file via HTTP PUT in PHP

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. 我正在尝试通过HTTP-PUT将文件发送到eXist数据库。 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 我有要将文档输入的URL我有eXist DB的用户名和密码我有需要通过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. 我尝试使用cURL,但它会无声地失败我尝试使用PHP流,但不断收到“错误201 /创建”但实际上没有创建文件。

Any help with this would be GREATLY appreciated. 任何有关这方面的帮助都将非常感激。

Here's some sample code I tried using PHP streams 这是我尝试使用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);

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

CURL works for me. CURL适合我。 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. 我自己不使用身份验证,因此未经过测试。

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. 许多标题行可能不是必需的......但是当我和我的couchdb交谈时它会起作用,所以我没有把它们除掉。

如果您的eXist-db启用了SOAP接口,那么就会有一个名为PheXist的开源库,可以更轻松地与数据库进行交互。

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/ 来自http://www.littlehart.net/atthekeyboard/2008/01/11/how-to-http-put-a-file-somewhere-using-php/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM