php using curl to get the html and save it on a txt

I've been learning to program in PHP and i am trying to make a PHP script with CURL instead file_get_contents($url)

The reason that i want to learn more about Curl is because it is faster and more flexile but more harder

So, i am coding this to download the HTML code from some URL and save it on a TXT

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.google.pt");
curl_setopt($ch, CURLOPT_HEADER, 0);

//should save html in $html
$html = curl_exec($ch);

//save html on txt
$fh = fopen('html.txt', 'w') or die("can't open file");
        fwrite($fh, $html);
        fclose($fh);

curl_close($ch);

But instead, curl_exec(); return 1 and print the code on my page

is there anyway to do what i want?

Upvotes: 2

Views: 3391

Answers (1)

Kohjah Breese
Kohjah Breese

Reputation: 4136

You need to use:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );

Otherwise the result is just printed out.

Upvotes: 5

Related Questions