当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP获取网页所有连接的方法(附demo源码下载)

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

西海都市报,过路费英文,linux重启命令

本文实例讲述了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;

完整实例代码点击此处。

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网