卷曲发送内容长度标头时出现问题

发布于 2024-11-30 10:48:31 字数 3091 浏览 1 评论 0原文

我需要将文件上传到 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

我认为我发送了内容长度,cookie 和文件都正常。 更清楚地说,这是这次调用的curl日志

* 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-12-07 10:48:31

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

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

夜夜流光相皎洁 2024-12-07 10:48:31

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

curl_setopt($ch, CURLOPT_UPLOAD, true); 

i removed that from the function and now it works

curl_setopt($ch, CURLOPT_UPLOAD, true); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文