简体   繁体   中英

Accessing and printing HTML source code using PHP or JavaScript

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 accomplish 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.com';
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.

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.com');
$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. :)

您应该尝试在<pre></pre>标签之间打印结果;

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.com/', true); // Source code with lines
echo get_data('https://www.google.com/'); // Source code without lines
?>

Hope it gets you on your way.

添加标题Content-Type:文本/纯文本

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM