1

I'm trying to get content of a specific URL using cURL

$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, 
   "http://stackoverflow.com//");
$output= curl_exec($ch);
curl_close($ch);

Now I want to display markup just as text, I'm write the following code snippet:

<div class="content">
    <pre>
       <?php
            echo $output;
        ?>
    </pre>
</div> 

But when I'm trying to display markup there is no div, pre and others my markup's element. I can see the stackoveflow.com only. How to fix this?

  • @Quentin There is no executing of `echo $output` after executing curl request. Why? –  Feb 11 '14 at 07:39

1 Answers1

2

You need to surround your $output with htmlentities() function to render tag output on your page.

Alternatively, you could make use of htmlspecialchars() as the former is pretty heavy.

So it would be like

<div class="content">
    <pre>
       <?php
            echo htmlentities($output);
        ?>
    </pre>
</div> 

EDIT :

It doesn't work. This page is rendered as before.

That is because you need to set the RETURNTRANSFER cURL parameter to 1.

The code...

<?php
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,"http://stackoverflow.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$output= curl_exec($ch);
curl_close($ch);
?>
<div class="content">
    <pre>
       <?php
       echo htmlentities($output);
       ?>
    </pre>
</div> 
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126