免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2362 | 回复: 0
打印 上一主题 下一主题

PHP Cookbook读书笔记 – 第13章Web自动化 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-25 14:40 |只看该作者 |倒序浏览
PHP Cookbook读书笔记 – 第13章Web自动化








通过GET获得一个指定url的页面内容
有3种方式来获取一个URL的内容:
  1. 1.PHP提供的文件函数file_get_contents()
  2. 2.cURL扩展
  3. 3.PEAR中的HTTP_Request类
  4. view sourceprint?
  5. 01 //方式1  

  6. 02 $page = file_get_contents('http://www.example.com/robots.txt');  

  7. 03   

  8. 04 //方式2  

  9. 05 $c = curl_init('http://www.example.com/robots.txt');  

  10. 06 curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  

  11. 07 $page = curl_exec($c);  

  12. 08 curl_close($c);  

  13. 09   

  14. 10 //方式3  

  15. 11 require_once 'HTTP/Request.php';  

  16. 12 $r = new HTTP_Request('http://www.example.com/robots.txt');  

  17. 13 $r->sendRequest();  

  18. 14 $page = $r->getResponseBody();
复制代码
可以通过这些方式来获取XML文档,通过结合http_build_query()来建立一个查询字符串,可以通过url中加入username@password的形式来访问受保护的页面,通过cURL和PEAR的HTTP_Client类来跟踪重定向。

通过POST获得一个URL
让PHP模拟发送一个POST请求并获得服务器的反馈内容
  1. view sourceprint?
  2. 01 //1  

  3. 02 $url = 'http://www.example.com/submit.php';  

  4. 03 $body = 'monkey=uncle&rhino=aunt';  

  5. 04 $options = array('method' => 'POST', 'content' => $body);  

  6. 05 $context = stream_context_create(array('http' => $options));  

  7. 06 print file_get_contents($url, false, $context);  

  8. 07   

  9. 08 //2  

  10. 09 $url = 'http://www.example.com/submit.php';  

  11. 10 $body = 'monkey=uncle&rhino=aunt';  

  12. 11 $c = curl_init($url);  

  13. 12 curl_setopt($c, CURLOPT_POST, true);  

  14. 13 curl_setopt($c, CURLOPT_POSTFIELDS, $body);  

  15. 14 curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  

  16. 15 $page = curl_exec($c);  

  17. 16 curl_close($c);  

  18. 17   

  19. 18 //3  

  20. 19 require 'HTTP/Request.php';  

  21. 20 $url = 'http://www.example.com/submit.php';  

  22. 21 $r = new HTTP_Request($url);  

  23. 22 $r->setMethod(HTTP_REQUEST_METHOD_POST);  

  24. 23 $r->addPostData('monkey','uncle');  

  25. 24 $r->addPostData('rhino','aunt');  

  26. 25 $r->sendRequest();  

  27. 26 $page = $r->getResponseBody();
复制代码
通过Cookie获得一个URL
  1. view sourceprint?
  2. 01 //2  

  3. 02 $c = curl_init('http://www.example.com/needs-cookies.php');  

  4. 03 curl_setopt($c, CURLOPT_COOKIE, 'user=ellen; activity=swimming');  

  5. 04 curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  

  6. 05 $page = curl_exec($c);  

  7. 06 curl_close($c);  

  8. 07   

  9. 08 //3  

  10. 09 require 'HTTP/Request.php';  

  11. 10 $r = new HTTP_Request('http://www.example.com/needs-cookies.php');  

  12. 11 $r->addHeader('Cookie','user=ellen; activity=swimming');  

  13. 12 $r->sendRequest();  

  14. 13 $page = $r->getResponseBody();
复制代码
通过Header获得一个URL
通过修改header中的信息可以来伪造 Referer 或 User-Agent 后请求目标URL,不少防盗链网站经常会采用判断Referer中的信息来源决定是否允许下载或访问资源。需要具备一些HTTP的HEADER背景知识。

标记网页
其实这个代码经过简单修改还可以应用到替换网页中的敏感关键字,这在天朝是很有用的一个功能
  1. view sourceprint?
  2. 1 $body = '
  3. I like pickles and herring.

  4. view sourceprint?
  5. 01 <A href="http://www.cnblogs.com/Excellent/admin/pickle.php"><IMG alt="" src="http://www.cnblogs.com/Excellent/admin/pickle.jpg">A pickle picture</A>  

  6. 02   

  7. 03 I have a herringbone-patterned toaster cozy.  

  8. 04   

  9. 05 Herring is not a real HTML element!  

  10. 06 ';  

  11. 07   

  12. 08 $words = array('pickle','herring');  

  13. 09 $patterns = array();  

  14. 10 $replacements = array();  

  15. 11 foreach ($words as $i => $word) {  

  16. 12     $patterns[] = '/' . preg_quote($word) .'/i';  

  17. 13     $replacements[] = "<SPAN class=word-$i>\\0</SPAN>";  

  18. 14 }  

  19. 15   

  20. 16 // Split up the page into chunks delimited by a  

  21. 17 // reasonable approximation of what an HTML element  

  22. 18 // looks like.  

  23. 19 $parts = preg_split("{(<(?:\"[^\"]*\"|'[^']*'|[^'\">])*>)}",  

  24. 20                     $body,  

  25. 21                     -1,  // Unlimited number of chunks  

  26. 22                     PREG_SPLIT_DELIM_CAPTURE);  

  27. 23 foreach ($parts as $i => $part) {  

  28. 24     // Skip if this part is an HTML element  

  29. 25     if (isset($part[0]) && ($part[0] == '<')) { continue; }  

  30. 26     // Wrap the words with <SPAN>s  

  31. 27     $parts[$i] = preg_replace($patterns, $replacements, $part);  

  32. 28 }  

  33. 29   

  34. 30 // Reconstruct the body  

  35. 31 $body = implode('',$parts);  

  36. 32   

  37. 33 print $body;</SPAN>
复制代码
提取页面所有链接
也是一个很不错的功能,在做采集之类的程序时可以用的上

采用了tidy扩展的实现方式:
  1. view sourceprint?
  2. 01 $doc = new DOMDocument();  

  3. 02 $opts = array('output-xml' => true,  

  4. 03               // Prevent DOMDocument from being confused about entities  

  5. 04               'numeric-entities' => true);  

  6. 05 $doc->loadXML(tidy_repair_file('linklist.html',$opts));  

  7. 06 $xpath = new DOMXPath($doc);  

  8. 07 // Tell $xpath about the XHTML namespace  

  9. 08 $xpath->registerNamespace('xhtml','http://www.w3.org/1999/xhtml');  

  10. 09 foreach ($xpath->query('//xhtml:a/@href') as $node) {  

  11. 10     $link = $node->nodeValue;  

  12. 11     print $link . "\n";
复制代码
通过正则提取链接:
  1. view sourceprint?
  2. 1 $html = file_get_contents('linklist.html');  

  3. 2 $links = pc_link_extractor($html);  

  4. 3 foreach ($links as $link) {  

  5. 4     print $link[0] . "\n";  

  6. 5 }  

  7. 6   

  8. 7 function pc_link_extractor($html) {  

  9. 8     $links = array();  

  10. 9     preg_match_all('/<A href="http://www.cnblogs.com/Excellent/admin/[\"\']?([^\"\'">]*)[\"\']?[^>]*>(.*?)<\/a>/i', $html,$matches,PREG_SET_ORDER); foreach($matches as $match) { $links[] = array($match[1],$match[2]); } return $links;</A>
复制代码
将文本转换为HTML
bbcode的概念和这个很像,所以将这个贴出来
  1. view sourceprint?
  2. 1 function pc_text2html($s) {  

  3. 2   $s = htmlentities($s);  

  4. 3   $grafs = split("\n\n",$s);  

  5. 4   for ($i = 0, $j = count($grafs); $i < $j; $i++) {  

  6. 5     // 转换html超链接  

  7. 6     $grafs[$i] = preg_replace('/((ht|f)tp:\/\/[^\s&]+)/',  

  8. 7                               '<A href="http://www.cnblogs.com/Excellent/admin/$1">$1</A>',$grafs[$i]);    // 转换email链接  

  9. 8     $grafs[$i] = preg_replace('/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i',        '<A href="mailto:$1">$1</A>',$grafs[$i]);    // 开始一个新段落  

  10. 9     $grafs[$i] = '
  11. '.$grafs[$i].'

  12. view sourceprint?
  13. 1 ';  }  return implode("\n\n",$grafs);}
复制代码
将HTML转换为文本
已经有现成的代码来实现这个功能http://www.chuggnutt.com/html2text.php

删除HTML和PHP标签
用这个函数strip_tags( ) 可以删除HTML和PHP标签
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP