Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304252
  • 博文数量: 111
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 707
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-26 11:00
个人简介

小伙向前冲呀,小伙向前冲呀。

文章分类

全部博文(111)

文章存档

2014年(43)

2013年(68)

我的朋友

分类: PHP

2014-01-03 06:36:23

PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯。libcurl目前支持http、https、ftp、 gopher、telnet、dict、file和ldap协议。

libcurl同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。 

php的curl真的是相当好用,网上一搜索相关文章都是关于curl模拟登陆的,很少人提供模拟discuz发贴的源码。 

本文原文地址为:php curl模拟登录discuz并模拟发帖

例子:

Java代码 
  1. $discuz_url = 'http://127.0.0.1/discuz/';//论坛地址   
  2. $login_url = $discuz_url .'logging.php?action=login';//登录页地址   
  3.   
  4. $post_fields = array();   
  5. //以下两项不需要修改   
  6. $post_fields['loginfield'] = 'username';   
  7. $post_fields['loginsubmit'] = 'true';   
  8. //用户名和密码,必须填写   
  9. $post_fields['username'] = 'tianxin';   
  10. $post_fields['password'] = '111111';   
  11. //安全提问   
  12. $post_fields['questionid'] = 0;   
  13. $post_fields['answer'] = '';   
  14. //@todo验证码   
  15. $post_fields['seccodeverify'] = '';   
  16. //获取表单FORMHASH   
  17. $ch = curl_init($login_url);   
  18. curl_setopt($ch, CURLOPT_HEADER, 0);   
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  20. $contents = curl_exec($ch);   
  21. curl_close($ch);   
  22. preg_match('//i', $contents, $matches);   
  23. if(!empty($matches)) {   
  24. $formhash = $matches[1];   
  25. else {   
  26. die('Not found the forumhash.');   
  27. }   
  28.   
  29. //POST数据,获取COOKIE,cookie文件放在网站的temp目录下   
  30. $cookie_file = tempnam('./temp','cookie');   
  31. $ch = curl_init($login_url);   
  32. curl_setopt($ch, CURLOPT_HEADER, 0);   
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  34. curl_setopt($ch, CURLOPT_POST, 1);   
  35. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);   
  36. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);   
  37. curl_exec($ch);   
  38. curl_close($ch);   
  39. //取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID   
  40. $send_url = $discuz_url."post.php?action=newthread&fid=2";   
  41.   
  42. $ch = curl_init($send_url);   
  43. curl_setopt($ch, CURLOPT_HEADER, 0);   
  44. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  45. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);   
  46. $contents = curl_exec($ch);   
  47. curl_close($ch);   
  48. //这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性   
  49. preg_match('//i', $contents, $matches);   
  50. if(!empty($matches)) {   
  51. $formhash = $matches[1];   
  52. else {   
  53. die('Not found the forumhash.');   
  54. }   
  55.   
  56. $post_data = array();   
  57. //帖子标题   
  58. $post_data['subject'] = 'test2';   
  59. //帖子内容   
  60. $post_data['message'] = 'test2';   
  61. $post_data['topicsubmit'] = "yes";   
  62. $post_data['extra'] = '';   
  63. //帖子标签   
  64. $post_data['tags'] = 'test';   
  65. //帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确   
  66. $post_data['formhash']=$formhash;   
  67.   
  68. $ch = curl_init($send_url);   
  69. curl_setopt($ch, CURLOPT_REFERER, $send_url); //伪装REFERER   
  70. curl_setopt($ch, CURLOPT_HEADER, 0);   
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);   
  72. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);   
  73. curl_setopt($ch, CURLOPT_POST, 1);   
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);   
  75. $contents = curl_exec($ch);   
  76. curl_close($ch);   
  77. //清理cookie文件   
  78. unlink($cookie_file);   
  79. ?>   


更多有关php curl的文章,请参考:

curl获取网站的http的状态码


php curl上传文件
php curl应用实例
php curl伪造IP来源
php curl 学习总结

阅读(1169) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~