Problem with curl to send Content-length header

2019-02-28 06:22发布

问题:

i need to upload files to wupload here is my function

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

and i tried to make this call

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

and the result is

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

i thought i sent the content lenght, cookies and file are ok. to be more clear, here is the curl log for this call

* 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

回答1:

When you upload with PUT (which CURLOPT_UPLOAD implies) you should set the size of the file to be uploaded with CURLOPT_INFILESIZE. (Or possibly the one made to support really large files: CURLOPT_INFILESIZE_LARGE).



回答2:

i removed that from the function and now it works

curl_setopt($ch, CURLOPT_UPLOAD, true);