search
Homephp教程php手册PHP中使用CURL获取页面title例子,curltitle

PHP中使用CURL获取页面title例子,curltitle

通过PHP获取页面title内容的实战演示:

范例代码:

复制代码 代码如下:


/* 
功能: 取得 URL 页面上的

内容   <br>   <br> 参数:$_POST['url']  <br> */   <br>    <br> // 设置最长执行的秒数   <br> ini_set ("expect.timeout", 30);   <br> set_time_limit(30);   <br>    <br> // 检查 URL   <br> if(!isset($_POST['url']) || $_POST['url'] == ''){    <br>    echo "URL 错误";   <br>    exit;   <br> }   <br>    <br>    <br> /* 取得 URL 页面数据 */   <br> // 初始化 CURL   <br> $ch = curl_init();   <br>    <br> // 设置 URL    <br> curl_setopt($ch, CURLOPT_URL, $_POST['url']);    <br> // 让 curl_exec() 获取的信息以数据流的形式返回,而不是直接输出。   <br> curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   <br> // 在发起连接前等待的时间,如果设置为0,则不等待   <br> curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);   <br> // 设置 CURL 最长执行的秒数   <br> curl_setopt ($ch, CURLOPT_TIMEOUT, 30);   <br>    <br> // 尝试取得文件内容   <br> $store = curl_exec ($ch);   <br>    <br>    <br> // 检查文件是否正确取得   <br> if (curl_errno($ch)){   <br>    echo "无法取得 URL 数据";   <br>    //echo curl_error($ch);/*显示错误信息*/   <br>    exit;   <br> }   <br>    <br> // 关闭 CURL   <br> curl_close($ch);   <br>    <br>    <br> // 解析 HTML 的 区段   <br> preg_match("/<head.>(.*)/smUi",$store, $htmlHeaders);   <br> if(!count($htmlHeaders)){   <br>    echo "无法解析数据中的 区段";   <br>    exit;   <br> }       <br>       <br> // 取得 中 meta 设置的编码格式   <br> if(preg_match("/<meta>]*http-equiv[^>]*charset=(.*)(\"|')/Ui",$htmlHeaders[1], $results)){   <br>    $charset =  $results[1];   <br> }else{    <br>    $charset = "None";   <br> }   <br>    <br> // 取得 <title> 中的文字    <br> if(preg_match("/<title>(.*)/Ui",$htmlHeaders[1], $htmlTitles)){   <br>    if(!count($htmlTitles)){   <br>        echo "无法解析 <title> 的内容";   <br>        exit;   <br>    }   <br>       <br>    // 将  <title> 的文字编码格式转成 UTF-8   <br>    if($charset == "None"){   <br>        $title=$htmlTitles[1];   <br>    }else{   <br>        $title=iconv($charset, "UTF-8", $htmlTitles[1]);   <br>    }   <br>    echo $title;   <br> }   <br>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool