Accessing and printing HTML source code using PHP or JavaScript - Stack Overflow

I am trying to access and then print (or just be able to use) the source code of any website using PHP.

I am trying to access and then print (or just be able to use) the source code of any website using PHP. I am not very experienced and am now thinking I might need to use JS to acplish this. So far, the code below accesses the source code of a web page and displays the web page... What I want it to do instead is display the source code. Essentially, and most importantly, I want to be able to store the source code in some sort of variable so I can use it later. And eventually read it line-by-line - but this can be tackled later.

$url = '';
function get_data($url) 
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
echo get_data($url); //print and echo do the same thing in this scenario.

I am trying to access and then print (or just be able to use) the source code of any website using PHP. I am not very experienced and am now thinking I might need to use JS to acplish this. So far, the code below accesses the source code of a web page and displays the web page... What I want it to do instead is display the source code. Essentially, and most importantly, I want to be able to store the source code in some sort of variable so I can use it later. And eventually read it line-by-line - but this can be tackled later.

$url = 'http://www.google.';
function get_data($url) 
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
echo get_data($url); //print and echo do the same thing in this scenario.
Share Improve this question edited Jul 27, 2015 at 19:23 durron597 32.3k18 gold badges104 silver badges160 bronze badges asked Dec 27, 2012 at 22:33 rwbarg15rwbarg15 3292 gold badges5 silver badges11 bronze badges 2
  • You can use document.body.innerHTML, pretty simple, isn't it? – gdoron Commented Dec 27, 2012 at 22:35
  • Can you explain this further? Maybe give an example? @gdoron – rwbarg15 Commented Dec 27, 2012 at 23:13
Add a ment  | 

5 Answers 5

Reset to default 2

Consider using file_get_contents() instead of curl. You can then display the code on your page by replacing every opening bracket (<) with &lt; and then outputting it to the page.

<?php
$code = file_get_contents('http://www.google.');
$code = str_replace('<', '&lt;', $code);
echo $code;
?>

Edit:
Looks like curl is actually faster than FGC, so ignore that suggestion. The rest of my post still stands. :)

You should try to print the result between <pre></pre> tags;

echo '<pre>' . get_data($url) . '</pre>';

I rewrote your function. The function can return the source with lines or without lines.

<?php 
function get_data($url, $Addlines = false){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    $content = curl_exec($ch);
    $content = htmlspecialchars($content); // Prevents the browser to parse the html

    curl_close($ch);

    if ($Addlines == true){
        $content = explode("\n", $content);
        $Count = 0;
        foreach ($content as $Line){
            $lines = $lines .= 'Line '.$Count.': '.$Line.'<br />';
            $Count++;
        }
        return $lines;
    } else {
        $content = nl2br($content);
        return $content;
    }
}


echo get_data('https://www.google./', true); // Source code with lines
echo get_data('https://www.google./'); // Source code without lines
?>

Hope it gets you on your way.

Add a header Content-Type: text/plain

header("Content-Type: plain/text"); 

Use htmlspecialchars() in php to print the source code.

In your code, use

return htmlspecialchars($data);

instead of

return $data;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356971a4624177.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信