0

I have this code

<?php
function get(){
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com/");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $c = curl_exec($ch);
        curl_close($ch);
        die("A");
        return $c;
}

$first = get();


?>

but look what's output! It just prints off everything returned from curl_exec();

how is that possible?

5
  • @downvoter: Can you at least say what is wrong with this question ? Commented Jul 15, 2011 at 20:02
  • The question is so simple, some think it hurts. See the answer. Commented Jul 15, 2011 at 20:05
  • 2
    Hey I'd recommend not printing out the stackoverflow page in an SO question. It feels like phishing and confuses the shit out of ppl regardless :) Commented Jul 15, 2011 at 20:06
  • if you click to /login it throws you 404 so I think you/I don't have to worry about that ;) Commented Jul 15, 2011 at 20:10
  • however your're right. Added warning at the very top of that demo Commented Jul 15, 2011 at 20:13

1 Answer 1

2

Yes (as you already have tested).

curl_exec PHP Manual does return output directly to the browser unless you make use of the CURLOPT_RETURNTRANSFER constant as documented.

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

7 Comments

It does not make sense. There is "Returns TRUE on success or FALSE on failure. " but there isn't "Outputs result on success or returns FALSE on failure. " However it works. Thanks
@genesis: it does make sense. turning on returntransfer makes curl_exec RETURN the fetched data, instead of directly outputting it.
@genesis: Looks like you have overseen it: 1.) Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. - so read the whole text that is provided under Return Values - always. And 2.) next to that there is a code example below on that page that has a comment next to the curl_exec line that states: grab URL and pass it to the browser. So next tip is to read the examples.
yup, you're right. Why did you put this post to community wiki ?
Because the answer was so obvious that there's only one answer. If other folks want to contribute to it, that makes more sense than if everybody is answering nearly the same but on their own. And it protects me and others from downvotes on the answer, so some little precaution in this great browser game.
|

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.