0

I have writen a php curl function for downloading a web page from an ssl enable site. It is no working as I want it to. It echos itself even if I have not used the echo command elsewhere. I want to use this function to save the download page but it saves 1 file not the whole data.

I already googled about this issue but had no success. Writing on stack overflow is always last option for me and I always avoid due to my bad english as well as bad explanation

function gssl($target_url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,true);
    curl_setopt($ch, CURLOPT_CAINFO, 'C:\Users\PC\Desktop\ssl\cacert-2019-05-15.pem');
    //curl_setopt($ch, CURLOPT_CAINFO, 'https://curl.haxx.se/ca/cacert-2019-05-15.pem');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_VERBOSE,true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    $html= curl_exec($ch);
    return $html;
    }

function write($post,$myFile){
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $post);
fclose($fh);
}
Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33

2 Answers2

1

Set CURLOPT_RETURNTRANSFER to true

James Bond
  • 2,229
  • 1
  • 15
  • 26
0

You are returning the result of curl_exec($ch); not the page from the web site. Use the CURLOPT_FILE cURL option to have the returned data put directly into a file.

And, as James said, enable CURLOPT_RETURNTRANSFER too.

Dave
  • 5,108
  • 16
  • 30
  • 40