且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP中的curl从URL获取HTML

更新时间:2023-02-23 14:58:20

如果我正确理解了您的要求,以下脚本将带您到达那里。您可以使用 htmlspecialchars()来获得所需的功能

If I correctly understood your requirement, the following script should get you there. There is a function you can make use of htmlspecialchars() to get the desired output.

<?php
function get_content($url) {
    $options = array(
            CURLOPT_RETURNTRANSFER => 1, 
            CURLOPT_USERAGENT      => "Mozilla/5.0",         
    );
    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $htmlContent = curl_exec( $ch );
    curl_close( $ch );
    return $htmlContent;
}
$link = "https://***.com/questions/52477020/get-html-from-a-url-using-curl-in-php"; 
$response = get_content($link);
echo htmlspecialchars($response);
?>

我在脚本中使用的链接只是一个占位符。随时用您想要的那个替换它。

The link I've used within the script is just a placeholder. Feel free to replace that with the one you are after.