【问题标题】:Curl file upload with multipart/form-data使用 multipart/form-data 上传卷曲文件
【发布时间】:2012-09-09 20:15:34
【问题描述】:

我在使用 curl 上传文件时遇到问题。我想在视频托管网站上上传我的服务器文件。该脚本需要Content-type: multipart/form-data 和Content-type: video/mp4,但我不知道该怎么做。上传文件后有content type application/octet-stream。

这是脚本

class curl
{
         function __construct($use = 1)
         {
         $this->ch = curl_init();
                 if($use = 1)
                 {
                         curl_setopt ($this->ch, CURLOPT_POST, 1);
                         curl_setopt ($this->ch, CURLOPT_COOKIEJAR, 'cookie.txt');
                         curl_setopt ($this->ch, CURLOPT_FOLLOWLOCATION, 1);
                         curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1);
                 }
                 else
                 {
                         return 'There is the possibility, that this script wont work';
                 }
         }
         function first_connect($loginform,$logindata)
         {
                 curl_setopt($this->ch, CURLOPT_URL, $loginform);
                 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $logindata);
         }
         function store()
         {
                 $this->content = curl_exec ($this->ch);
         }
         function execute($page)
         {
                 curl_setopt($this->ch, CURLOPT_URL, $page);
                 $this->content = curl_exec ($this->ch);
         }
         function close()
         {
                 curl_close ($this->ch);
         }

         function __toString()
         {
         return $this->content;
         }

         function upload($file)
         {
            curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
            curl_setopt($this->ch, CURLOPT_URL, 'http://uploadsite.com/index.php?act=files&do=upload');
            $postdata = array("file[]" => "@/".realpath($file));
            echo $postdata;
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postdata);
            curl_exec ($this->ch);
         }
}


$getit = new curl();
$getit->first_connect('http://uploadsite.com/index.php?act=login&login=true','login=true&file_session=&email=xxx%40yahoo.de&password=xxx&upload=Login');
$getit->store();
$getit->execute('http://uploadsite.com/index.php?act=user&do=home');
$getit->upload('Sample.mp4');

还有来自livehttpheaders的包裹

http://uploadsite.com/index.php?act=files&do=upload

POST /index.php?act=files&do=upload HTTP/1.1
Host: yourupload.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/javascript, text/html, application/xml, text/xml, */*
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://yourupload.com/index.php?act=user&do=home
Content-Length: 41648931
Content-Type: multipart/form-data; boundary=---------------------------265001916915724
Cookie: _pk_ref.9.d1ba=%5B%22%22%2C%22%22%2C1347200605%2C%22http%3A%2F%2F62.75.242.162%2Findex.php%22%5D; _pk_id.9.d1ba=08e499ca1b88c81c.1345157523.12.1347203814.1347189831.; PHPSESSID=jjajqi2mi7fe1blso5qvge9ue5; _pk_ses.9.d1ba=*; member_id=3051; member_pass=xxx
Pragma: no-cache
Cache-Control: no-cache
-----------------------------265001916915724
Content-Disposition: form-data; name="file[]"; filename="Sample.mp4"
Content-Type: video/mp4


HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Sun, 09 Sep 2012 15:23:56 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.3.3-7+squeeze13
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

【问题讨论】:

    标签: php curl upload


    【解决方案1】:

    替换这一行:

    $postdata = array("file[]" => "@/".realpath($file));
    

    与:

    $postdata = array("file[]" => "@/".realpath($file).";type=video/mp4");
    

    它应该可以工作。

    【讨论】:

      【解决方案2】:

      我使用 curl 连接 Web 服务并使用与本文类似的想法上传文件,但它不起作用。与上一个答案不同的原因,所以我将描述我的解决方案;也许我的经验对其他人有用。

      我有两个问题。第一个是我的网络服务不喜欢文件名中的绝对路径。如果 OP 代码中的 $file 有路径,则使用:

      $postdata = array('file[]' => "@/".realpath($file));
      

      变成:

      Content-Disposition: form-data; name="file[]"; filename="/tmp/iy56ham"
      

      并且该路径生成了错误的请求。所以我不得不使用 chdir、basename 和 dirname 以避免在 $postdata 中使用路径。

      另一方面,curl 发送了一个额外的标头,OP 中没有显示:

      Expect: 100-continue
      

      我的网络服务不喜欢它(twitter WS 也不喜欢),它的回答是:

      417 Expectation Failed
      

      为避免 curl 发送有问题的标头,您可以使用:

      curl_setopt( $curlHandler, CURLOPT_HTTPHEADER, array('Expect:') );
      

      所以你设置一个空的 Expect 标头并且 curl 不会被 100-continue 覆盖。

      希望这对某人有所帮助...

      【讨论】:

        猜你喜欢
        • 2015-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-01
        • 2016-12-14
        相关资源
        最近更新 更多