4

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.

Here is my code.

$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;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Haq Nawaz
  • 412
  • 1
  • 4
  • 12

1 Answers1

1

Latest update!

You have more than 1 errors in your codes,

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

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

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');

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';, 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.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 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 error 28: Connection timed out

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

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

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

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

So basicaly!

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.
  • @HaqNawaz update question and show what you did please –  Jan 26 '20 at 17:25
  • I am running the same code on localhost which is working and puling the data but on server it's not working even it's not showing any error, and I did enabled error reporting and increased execution time but nothing is showing. – Haq Nawaz Jan 26 '20 at 17:33
  • Localhost and live servers are working different, 1 of them is `SSL` localhost doesnt require, but live servers does, `CURLOPT_RETURNTRANSFER` should be enabled on live but not required on local, http headers might be required on live server, there are tons of things I can tell, so you want to make it work ? edit your question and add latest code you tried. and **use the example in my question** its working fine I tested it peronaly. and add others step by step, so you can find what is wrong, we cant have a guess about your setup and your codes if you dont show us and not trying what we say. –  Jan 26 '20 at 17:41
  • See This is well another function by me : https://stackoverflow.com/a/59422673/12232340 –  Jan 26 '20 at 19:19
  • I made changes on it and it's giving me 28 in error – Haq Nawaz Jan 27 '20 at 03:59
  • @HaqNawaz Nice atleast showing us something. you should try my code, error means timeout connection, so, 1.paste this `curl_close($ch);` under `$curl error_no` and remove `exit;` in you code, can be your curl is working infinite. 2.Set time out `curl_setopt($ch, CURLOPT_TIMEOUT, 500);`. 3. Some times headers causing that problem too, so, turn headers to false and see result if it causing a problem. I updated my answer, added timeout. and plase add latest code you tried and get error 28. –  Jan 27 '20 at 07:58
  • Thanks for answering I appreciate it. I also increased the timeout to 500 but then it's showing nothing curl_setopt($curl, CURLOPT_TIMEOUT, 500); – Haq Nawaz Jan 27 '20 at 12:07
  • @HaqNawaz try second code in answer as its dont change anything, just put path to your file and your login infos, and see if it works. –  Jan 27 '20 at 12:11