【问题标题】:cURL file download works on PHP 5.3 not on 5.5cURL 文件下载适用于 PHP 5.3 而不是 5.5
【发布时间】:2015-01-02 11:49:20
【问题描述】:

我有两台服务器在不同的软件版本中运行类似的网络应用程序。

两台服务器都运行 CentOS 6.5

一个有 Apache 2.2 php 5.3

另一个正在运行 Apache 2.4 php 5.5

此应用的主要功能之一是定期从远程 URL 下载 CSV 文件

这是使用 cURL 和以下代码完成的:

$filename = 'export.csv';
$url = 'http://www.someaddress.com/export/' . $filename;
$curl = curl_init();
$fd = fopen(DIR_FS_ADMIN . 'temp/' . $filename , "w");
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $fd);
curl_exec ($curl);
curl_close ($curl);

如您所见,一段非常简单的代码在 PHP 5.3 中运行良好

这是 curl_getinfo() 的结果

[content_type] => text/csv
[http_code] => 200
[header_size] => 209
[request_size] => 95
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.98925
[namelookup_time] => 0.816404
[connect_time] => 0.817009
[pretransfer_time] => 0.831392
[size_upload] => 0
[size_download] => 13564110
[speed_download] => 6818705
[speed_upload] => 0
[download_content_length] => 13564110
[upload_content_length] => -1
[starttransfer_time] => 0.834829
[redirect_time] => 0
[certinfo] => Array
    (
    )

[redirect_url] => 
)
Error Code: 0

这些是相同代码在 5.5 上运行的结果

[content_type] => 
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 126.332476
[namelookup_time] => 0.000369
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] => 
[primary_ip] => 
[certinfo] => Array
    (
    )

[primary_port] => 0
[local_ip] => 
[local_port] => 0
)
7 Failed to connect to www.someaddress.com/export/: Connection timed out

当然,在发布之前我已经研究并尝试了很多选项,增加超时时间,尝试资源的 SSL 版本,并使用不同的 curl_setopt 玩了很多,我总是无法从 5.5 连接应用程序。

我知道在 5.5 上对 cURL 扩展进行了一些更改,但我可以通过 Google 搜索找到上传问题,我还尝试了完全不同的选项,例如使用 file_get_contents,但仍然没有,只是超时。

两台服务器都位于同一个地方,并且 URL 是完全打开的,所以我真的怀疑问题出在文件位置,因为当我在 5.3 服务器上运行代码时仍然可以正常工作。

【问题讨论】:

    标签: php apache curl download php-5.5


    【解决方案1】:

    原来我试图访问的 URL 被我的服务器 IP 阻止了!

    我可以联系网站管理员并将我的 IP 地址列入白名单,现在代码可以正常工作,无需任何更改。

    还有一点需要记住,是什么让调试变得困难,它只是超时,没有错误消息或任何类型的错误消息。

    【讨论】:

      【解决方案2】:

      来自php手册

      现在仅当 CURLOPT_SAFE_UPLOAD 选项设置为 FALSE 时才支持使用 @file 语法进行上传。应该使用 CURLFile。

      curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); 
      

      多部分 POST 请求存在“@”问题。

          Solution for PHP 5.5 or later:
          - Enable CURLOPT_SAFE_UPLOAD.
          - Use CURLFile instead of "@".
      
          Solution for PHP 5.4 or earlier:
          - Build up multipart content body by youself.
          - Change "Content-Type" header by yourself.
      

      The CURLFile class

      【讨论】:

      • 我知道这一点,正如我在 cmets 中所说,我知道 CURLFile 类,但此文件对象仅用于从您已有的本地文件上传到服务器。但是,即使我采取 curl_setopt($curl, CURLOPT_FILE, $fd);退出代码并尝试获取内容,我仍然在资源上超时......
      • 我确实尝试过使用 CURLOPT_SAFE_UPLOAD 并没有运气,但我想我终于找到了问题,显然我试图获取请求的服务器正在拒绝来自我的服务器 IP 的所有内容,这可以解释为什么相同的代码可以在不同的服务器上运行,也可以从任何其他网站下载。感谢您的回答,网站管理员将我的 IP 列入白名单后,仍需尽快确认
      猜你喜欢
      • 2019-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 2013-10-30
      相关资源
      最近更新 更多