3

I'm using the following code:

$ch = curl_init('www.google.com'); $output = curl_exec($ch); curl_close($ch); 

This is not the first time I've used cURL, and unless I'm mistaken the above code should retrieve the contents of google.com and store it in $output. Correct?

So, why then, does the above code output the contents (in this example the Google homepage) to the page? I'm not echo'ing anything out, but for some reason the curl_exec() function is outputting what it returns to the page.

Am I missing something?

1 Answer 1

5

you need to use

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

That will tell curl_exec not to output the results

so change it all to

$ch = curl_init('www.google.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.