curl发送Content-length标头的问题

时间:2011-08-21 17:00:22

标签: php http curl header

我需要将文件上传到wupload 这是我的功能

public function doPost($link, $postfields = '', $cookie = '', $ref = '', $nobody = false, $upload = false, $header = true, $headers = "") {
    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($header)
        curl_setopt($ch, CURLOPT_HEADER, 1);
    if ($headers)
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_REFERER, $ref);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);  
    if ($postfields) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    }
    if ($cookie) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

    }
    $log = fopen("log.txt", "w+");
    curl_setopt($ch, CURLOPT_STDERR, $log);
    if ($upload) 
        curl_setopt($ch, CURLOPT_UPLOAD, true); 
    $page = curl_exec($ch);
    curl_close($ch);
    fclose($log);
    return($page);
}

我尝试拨打电话

    $result = doPost("http://www.wupload.com/file-manager/list", '', $this->upcookies, "http://www.wupload.com/", false);
    preg_match("/_uploadServerHostname = '(s[0-9]*.wupload.com)'/i", $result, $matches);
    $server = $matches[1];
    $post = array();
    $post['files[]'] = "@$filelocation";
    $headers = array('Content-Type: application/octet-stream','Content-length: ' . filesize($filelocation));
    $page = $this->doPost($server, $post, $this->upcookies, "http://www.wupload.com", false, true, true, $headers);

,结果是

HTTP/1.1 411 Length Required Server: nginx Date: Sun, 21 Aug 2011 16:10:51 GMT Content-Type: text/html Content-Length: 174 Connection: close

我以为我发送了内容长度,cookies和文件都可以。 更清楚的是,这是此次通话的卷曲日志

* About to connect() to s206.wupload.com port 80 (#0)
*   Trying 78.140.153.43... * connected
* Connected to s206.wupload.com (78.140.153.43) port 80 (#0)
> PUT / HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1
Host: s206.wupload.com
Accept: */*
Referer: http://www.wupload.com
Transfer-Encoding: chunked
Cookie: fs_orderFoldersDirection=asc; fs_orderFoldersBy=name; isAffiliate=1; nickname=soussouheros; email=avix%40live.fr; rememberMe=1; PHPSESSID=96aeh7liqt5p4u4d98at7acv71; role=premium; lang=en; visitSourceUrl=http%3A%2F%2Fwupload.com%2F; C:\wamp\www\tuto\brazzo/temp/wu.up

< HTTP/1.1 411 Length Required
< Server: nginx
< Date: Sun, 21 Aug 2011 16:10:51 GMT
< Content-Type: text/html
< Content-Length: 174
< Connection: close
< 
* Closing connection #0

2 个答案:

答案 0 :(得分:3)

使用PUT上传时(CURLOPT_UPLOAD暗示),您应该使用CURLOPT_INFILESIZE设置要上载的文件的大小。 (或者可能是支持非常大的文件:CURLOPT_INFILESIZE_LARGE)。

答案 1 :(得分:1)

我从函数中删除了它,现在它可以正常工作

curl_setopt($ch, CURLOPT_UPLOAD, true);