【问题标题】:cUrl working fine on localhost but it's not working on server and only shows blank pagecUrl 在 localhost 上工作正常,但在服务器上不起作用,只显示空白页
【发布时间】:2020-05-12 02:39:35
【问题描述】:

当我在服务器上运行以下代码时,它只显示空白页面并突然停止进一步执行,我还检查了已安装服务器上的 cUrl。

这是我的代码。

$ftp_server = 'ftps://'.'server/Voorraadtonen link.csv'; 
$ch = curl_init(str_replace(" ","%20",$ftp_server));
curl_setopt($ch, CURLOPT_URL, $ftp_server);
curl_setopt($ch, CURLOPT_USERPWD,'username'.':'.'password');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
//curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PORT, 990);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_VERBOSE,true);
$output = curl_exec($ch);
$error_no = curl_errno($ch);
echo $output; exit;

【问题讨论】:

  • 读取您的error_log文件并启用curl或php错误报告if(curl_errno($ch)){ throw new Exception(curl_error($ch)); }
  • 已经启用报错error_reporting(E_ALL); ini_set('display_errors', 1);
  • 你应该有错误,使用我在评论中给出的代码并阅读你的error_log文件
  • 我用过但没有显示
  • 你在$error_no有什么收获吗?

标签: php remote-access php-curl


【解决方案1】:

最新更新!

您的代码中有超过 1 个错误,

您在需要 SSL 验证的 url 中使用 FTPS,并且它在 你的代码。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//Dont use try! you shouldnt use
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);

他们应该是真的:SSL 不支持 true 所以他们应该像在另一个答案中关注@dharman warn。

但将 ssl 设为 true 将需要另一个设置,例如 cacert 文件 等等。

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//and include cacert.pem
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');

在此处下载 cacert 文件:https://curl.haxx.se/docs/caextract.html

2.您的网址不是真正的网址$ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';,此网址将一无所获,但它应该至少在error_log 文件中返回错误,正如您所说的所有错误报告都已启用

3.你的代码应该是这样的

$curl = curl_init();
$file = fopen("link.csv", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.site.com/link.csv"); 
//Make sure for correct url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
//Make sure for correct url
curl_setopt($curl, CURLOPT_FILE, $file); 
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//Make sure for your ftp credentials
curl_setopt($curl, CURLOPT_TIMEOUT, 20); //20 seconds will be enough
curl_exec($curl);
echo curl_errno($ch);
echo curl_error($ch);
curl_close($curl);
fclose($file);

不应该要求另外 1 个左侧标题,但如果需要的话。

curl_setopt($curl, CURLOPT_HEADER, false); //Or
curl_setopt($curl, CURLOPT_HEADER, true);

现在应该可以正常工作了

注意:示例代码是一个工作示例,您可以根据需要对其进行编辑

更新:修改后你说你在你的代码中做了(仍然没有向我们展示),最后我们得到一个错误。我再次要求您将修改后的代码添加到您的问题中。

Error_no 28 cURL 错误 28:连接超时

当 cURL 请求在一定时间内未完成时,会发生 cURL 28 错误。

当 cURL 超时值设置得太低或防火墙阻止 cURL 请求时,会发生这种情况。

另一种可能性是安全模块,例如 Apache mod_security 模块。

要修复 cURL 错误 28,您可以联系您的托管服务提供商。

所以基本上!

您的服务器正在阻塞。您的凭据与要求不匹配 凭据。服务器需要 SSL,但您没有设置它。 您的函数运行服务器内存限制设置的最大值。

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "ftp.site.com/link.csv"); 
//make sure your path to file is correct
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//make sure your login credentials correct
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
//Set timeout for connection
    curl_exec($curl);
    echo curl_errno($ch);
    echo curl_error($ch);
//Get errors
    curl_close($curl);
//Importand close curl connection.

【讨论】:

  • @HaqNawaz 更新问题并显示你做了什么
  • 我在 localhost 上运行相同的代码,它正在工作并提取数据,但在服务器上它不起作用,即使它没有显示任何错误,我确实启用了错误报告并增加了执行时间,但没有显示任何内容.
  • 本地主机和实时服务器的工作方式不同,其中 1 个是 SSL localhost 不需要,但实时服务器需要,CURLOPT_RETURNTRANSFER 应该在实时启用但在本地不需要,http 标头可能是在实时服务器上需要,我可以告诉很多事情,所以你想让它工作吗?编辑您的问题并添加您尝试过的最新代码。并且在我的问题中使用示例我对其进行了测试。并逐步添加其他人,以便您找到问题所在,如果您不向我们展示并且不尝试我们所说的,我们无法猜测您的设置和代码。
  • 看看这是我的另一个功能:stackoverflow.com/a/59422673/12232340
  • 我对其进行了更改,它给了我 28 个错误
猜你喜欢
  • 2016-10-28
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
相关资源
最近更新 更多