2

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?

1 Answer 1

5

You need to use:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );

Otherwise the result is just printed out.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks it work, but all my page content desapears, is that normal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.