curl通过代理上传文件返回错误

8

我想将一张图片文件上传到服务器。最初在家里没有使用代理测试我的脚本,它运行良好。但是当我在学校使用相同的脚本时,它会抛出一些错误。上传图片的函数如下:

function upload($filepath,$dir)

{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_PROXY, 'localhost:7777');
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'ae07b026:kpack');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, 'http://finalytics.in/sites/scrap/uploader.php' );
    $post_array = array(
        "my_file"=>"@".$filepath,
        "upload"=>"Upload",
        "dir"=>$dir
    );
    print_r($post_array);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); 
    $response = curl_exec($ch);
    echo $response; 

}

uploader.php 是一个普通的文件,只是用来保存图片。

我遇到的错误如下:

        ERROR
    The requested URL could not be retrieved

While trying to process the request:

    POST /sites/scrap/uploader.php HTTP/1.1
    Proxy-Authorization: Basic YWUwN2IwMjY6a3BhY2s=
    User-Agent: Mozilla/4.0 (compatible;)
    Host: finalytics.in
    Accept: */*
    Proxy-Connection: Keep-Alive
    Content-Length: 87022
    Expect: 100-continue
    Content-Type: multipart/form-data; boundary=----------------------------07ae68105e71


The following error was encountered:

    Invalid Request 

Some aspect of the HTTP Request is invalid. Possible problems:

    Missing or unknown request method
    Missing URL
    Missing HTTP Identifier (HTTP/1.0)
    Request is too large
    Content-Length missing for POST or PUT requests
    Illegal character in hostname; underscores are not allowed 

Your cache administrator is webmaster.
Generated Sun, 05 Jun 2011 17:26:33 GMT by proxy1.iitm.ac.in (squid/2.7.STABLE7) 

只是猜测,你的大学代理是否真的在你的本地主机上? - konsolenfreddy
这不是,我正在使用学术代理进行隧道传输。 - Krishna Deepak
3个回答

12

问题在于我们学院使用的代理是"SQUID",而Squid不支持Expect: 100-continue。

最后,在我的选项中添加了这个选项。

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

一切运作良好。


1
如果上述解决方案不起作用,请添加以下内容:


curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data",'Expect:  '));

0

我们曾经遇到过类似的错误,添加头部Expect: 解决了代理错误。然而,问题实际上是curl本身的问题。版本号小于等于7.68的不起作用,而版本号大于等于7.70的可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接