Table of Contents
PHP获取网页所有连接的方法(附demo源码下载),demo源码下载
您可能感兴趣的文章:
Home php教程 php手册 PHP获取网页所有连接的方法(附demo源码下载),demo源码下载

PHP获取网页所有连接的方法(附demo源码下载),demo源码下载

Jun 13, 2016 am 08:42 AM
php smarty

PHP获取网页所有连接的方法(附demo源码下载),demo源码下载

本文实例讲述了PHP获取网页所有连接的方法。分享给大家供大家参考,具体如下:

function getHtml($url, $charset='utf-8')
{
  $curl = curl_init();
  //curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:192.168.168.1', 'CLIENT-IP:192.168.168.1'));//IP
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_REFERER, "");  //来路
  $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
  curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
  // 只需返回HTTP header
  // curl_setopt($curl, CURLOPT_HEADER, 1);
  // 页面内容我们并不需要
  // curl_setopt($curl, CURLOPT_NOBODY, 1);
  // 返回结果,而不是输出它
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $html = curl_exec($curl);
  //$info = curl_getinfo($curl);
  //echo var_dump($info);
  if ($html === false) {
    //echo "cURL Error: " . curl_error($ch);
    return '';
  }
  curl_close($curl);
  if ($charset != 'utf-8')
  {
    $html = iconv($charset, "UTF-8", $html);
  }
  return $html;
}
header("Content-type: text/html; charset=utf-8");
include('simple_html_dom.php');
// 要打开 extension=php_mbstring.dll
//$url = 'http://www.baidu.com/s?wd=kaka';
$url = 'http://www.163.com/';
$str_html = getHtml($url, 'gbk');
$html = str_get_html($str_html);
$links = $html->find('a');
foreach($links as $link)
{
  $txt = trim($link->plaintext);
  echo $link->href . '[' . $txt . ']<br>';
}
$html = null;

Copy after login

完整实例代码点击此处本站下载。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php获取网页上所有链接的方法
  • php 正则表达式提取网页超级链接url的函数
  • 简介WordPress中用于获取首页和站点链接的PHP函数
  • WordPress中获取页面链接和标题的相关PHP函数用法解析
  • php获取超链接文本内容的正则表达式(五种方法)
  • php正则表达式获取内容所有链接
  • PHP获取短链接跳转后的真实地址和响应头信息的方法
  • php 自写函数代码 获取关键字 去超链接
Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles