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

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')) . "rnContent-type: text/xmlrn",
                 'content' => file_get_contents($tmpFile)
             )
         );
         $ctx = stream_context_create($params);
         $response = @file_get_contents($url, false, $ctx);

         return ($response == '');

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.


This works for me...

function put($_server,$_file,$_data)
{
  $fp = @fsockopen ($_server, 80, $errno, $errstr, 30);
  if ($fp)
  {
    $p = "PUT $_file HTTP/1.0rn";
    $p.= "User-Agent: Mozilla/3.0 (Windows NT 5.0; U) Opera 7.21  [da]rn";
    $p.= "Host: $_serverrn";
    $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.1rn";
    $p.= "Accept-Language: da;q=1.0,en;q=0.9rn";
    $p.= "Accept-Charset: windows-1252, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1rn";
    $p.= "Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0rn";
    $p.= "Referer: http://www.nasa.gov/secret/flightplans.asprn";
    $p.= "Content-type: application/x-www-form-urlencodedrn";
    $p.= "Content-length: ".strlen($_data)."rn";
    $p.= "rn";
    $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.

链接地址: http://www.djcxy.com/p/87292.html

上一篇: 活着“在服务器响应中

下一篇: 通过PHP中的HTTP PUT发送文件