0

I have an API, when I run that API in chrome, it will get download directly excel sheet, but now I want to run that API in CURL php code in my server, how can I do that one.

I tried :

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 300);// if file large
    curl_setopt($ch, CURLOPT_URL, "MyAPI");

    $return=curl_exec($ch);
    print_r($return);
    echo "<br>";
    // the important part 
    $destination ="files.csv";
    if (file_exists( $destination)) {
        unlink( $destination);
    }
    $file=fopen($destination,"w+");
    fputs($file,$return);
    if(fclose($file))
    {
        echo "downloaded";
    }
    curl_close($ch);

file.csv coming into server but result is coming only 1,

print_r($return) result is 1 but actually I have 10 records in that excel sheet.

Could any one help on it please?

Thanks Sandeep

  • https://www.php.net/manual/en/function.file-get-contents.php – nice_dev Jul 02 '22 at 08:03
  • Add `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);` to get the contents of the request. – Markus Zeller Jul 02 '22 at 08:20
  • @MarkusZeller I tried this line in my code then I am getting empty repose. – MURALA SANDEEP Jul 02 '22 at 08:51
  • `curl_setopt($ch, CURLOPT_URL, "MyAPI");` is no valid URL. – Markus Zeller Jul 02 '22 at 09:01
  • @MarkusZeller thanks for your response and ```MyAPI``` has give for understand and In my case actual Api when I run in chrome browser it will directly downland excel sheet with 10 records, this Api need to run by curl and store that excel file in my server ,that I was trying to do – MURALA SANDEEP Jul 02 '22 at 09:10
  • Use [curl_error()](https://www.php.net/manual/en/function.curl-error) to see what's wrong. – Markus Zeller Jul 02 '22 at 09:13
  • @MarkusZeller thanks for giving the inputs ,I tried ```if(curl_exec($ch) === false){echo 'Curl error: ' . curl_error($ch); }else {echo 'Operation completed without any errors';}``` I not getting any error here – MURALA SANDEEP Jul 02 '22 at 09:21
  • You will never get a error info if curl_exec() returns 1. – Markus Zeller Jul 02 '22 at 09:31
  • @MarkusZeller after your input i went through some solution and I have written curl code and I am getting one error please check this link and give me solution that will help me more https://stackoverflow.com/questions/72893135/get-facebook-leads-by-using-api – MURALA SANDEEP Jul 08 '22 at 07:02

0 Answers0