cUrl 在 localhost 上工作正常,但在服务器上不起作用,只显示空白页 [英] cUrl working fine on localhost but it's not working on server and only shows blank page

查看:61
本文介绍了cUrl 在 localhost 上工作正常,但在服务器上不起作用,只显示空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I run the below code on server it only shows the blank page and suddenly stop further execution, I also checked the cUrl on server which is installed.

这是我的代码.

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

推荐答案

最新更新!

您的代码中有 1 个以上的错误,

You have more than 1 errors in your codes,

您在需要 SSL 验证的 url 中使用 FTPS,而在你的代码.

you are using FTPS in url which requires SSL verification, and its false in your codes.

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 不支持真,所以它们应该像在另一个答案中关注@dharman 警告一样.

They should be true : SSL doesnt support true so they should be like following on @dharman warn in another answer.

但是将 ssl 设为 true 需要另一个设置,例如 cacert 文件诸如此类

But turning ssl true will require another setup like cacert file etc. lik so

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

Download cacert file here : https://curl.haxx.se/docs/caextract.html

2.Your url is not a true url $ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';,这个 url 什么也得不到,但它应该返回一个error_log 文件中至少有错误,正如您所说的所有错误报告都已启用

2.Your url is not a true url $ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';, this url will get nothing, but it should return an error atleast in error_log file, as you said all errors reporting are enabled

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

3.Your code should look like this

$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 个不应该需要保留标题的东西,但如果需要的话.

1 more thing left headers should not be required but in case its required.

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

现在应该可以正常工作了

Now it should work without any problem

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

NOTE : Example code is a working example you can edit it to your requirements

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

UPDATE : After modification you said you did in your codes (Still not showing us), finaly we get an error. once again I am asking you to add modified code into your question.

Error_no 28 cURL 错误 28:连接超时

Error_no 28 cURL error 28: Connection timed out

cURL 28 错误发生在 cURL 请求未在一定时间内完成时.

the cURL 28 error occurs when the cURL request isn’t completed in a certain amount of time.

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

This happens when the cURL timeout value is set too low or when a firewall is blocking the cURL request.

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

Another possibility is a security module, for example the Apache mod_security module.

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

To fix the cURL error 28 you can contact your hosting provider.

基本上!

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

Your server is blocking. your credentials not match to required credentails. SSL is required by server, but you are not setting it up. Your function runing max of your Server Memory Limits settings.

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

这篇关于cUrl 在 localhost 上工作正常,但在服务器上不起作用,只显示空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆