国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Php CURL模擬登陸論壇并采集數據實例

2024-05-04 21:47:59
字體:
來源:轉載
供稿:網友

要模擬瀏覽器訪問網站,首選要學會觀察瀏覽器是如何發送http報文的,以及網站服務器返回給瀏覽器 是什么樣的內容,我推薦安裝一個國外人開發的httpwatch的軟件,最好搞個破解的版本,否則有些功能是使用不了的,這個軟件安裝完成之后是嵌入在 IE里的,啟動Record,在地址欄輸入網址后回車,它就會將瀏覽器和服務器之間的所有通訊掃描出來,讓你一覽無遺,關于這個軟件的使用在本文不做介紹.

模擬瀏覽器登陸應用開發,最關鍵的地方是突破登陸驗證,CURL技術不只支持http,還支持https,區別就在多了一層SSL加密傳輸,如果是要登陸 https網站,php記得要支持openssl,還是先拿一個例子來分析,代碼如下:

  1. <?php 
  2. $discuz_url = 'http://127.0.0.1/discuz/'; //論壇地址 
  3. $login_url = $discuz_url . 'logging.php?action=login'//登錄頁地址 
  4.  
  5. $post_fields = array(); 
  6. //以下兩項不需要修改 
  7. $post_fields['loginfield'] = 'username'
  8. $post_fields['loginsubmit'] = 'true'
  9. //用戶名和密碼,必須填寫 
  10. $post_fields['username'] = 'tianxin'
  11. $post_fields['password'] = '111111'
  12. //安全提問 
  13. $post_fields['questionid'] = 0; 
  14. $post_fields['answer'] = ''
  15. //@todo驗證碼 
  16. $post_fields['seccodeverify'] = ''
  17.  
  18. //獲取表單FORMHASH 
  19. $ch = curl_init($login_url); 
  20. curl_setopt($ch, CURLOPT_HEADER, 0); 
  21. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  22. $contents = curl_exec($ch); 
  23. curl_close($ch); 
  24. preg_match('/<inputs*type="hidden"s*name="formhash"s*value="(.*?)"s*/>/i'$contents$matches); 
  25. if (!emptyempty($matches)) { 
  26.     $formhash = $matches[1]; 
  27. else { 
  28.     die('Not found the forumhash.'); 
  29.  
  30. //POST數據,獲取COOKIE,cookie文件放在網站的temp目錄下 
  31. $cookie_file = tempnam('./temp''cookie'); 
  32.  
  33. $ch = curl_init($login_url); 
  34. curl_setopt($ch, CURLOPT_HEADER, 0); 
  35. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  36. curl_setopt($ch, CURLOPT_POST, 1); 
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
  38. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); 
  39. curl_exec($ch); 
  40. curl_close($ch); 
  41.  
  42. //取到了關鍵的cookie文件就可以帶著cookie文件去模擬發帖,fid為論壇的欄目ID 
  43. $send_url = $discuz_url . "post.php?action=newthread&fid=2"
  44.  
  45.  
  46. $ch = curl_init($send_url); 
  47. curl_setopt($ch, CURLOPT_HEADER, 0); 
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  49. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  50. $contents = curl_exec($ch); 
  51. curl_close($ch); 
  52.  
  53. //這里的hash碼和登陸窗口的hash碼的正則不太一樣,這里的hidden多了一個id屬性 
  54. preg_match('/<inputs*type="hidden"s*name="formhash"s*id="formhash"s*value="(.*?)"s*/>/i'$contents$matches); 
  55. if (!emptyempty($matches)) { 
  56.     $formhash = $matches[1]; 
  57. else { 
  58.     die('Not found the forumhash.'); 
  59.  
  60.  
  61. $post_data = array(); 
  62. //帖子標題 
  63. $post_data['subject'] = 'test2'
  64. //帖子內容 
  65. $post_data['message'] = 'test2'
  66. $post_data['topicsubmit'] = "yes"
  67. $post_data['extra'] = ''
  68. //帖子標簽 
  69. $post_data['tags'] = 'test'
  70. //帖子的hash碼,這個非常關鍵!假如缺少這個hash碼,discuz會警告你來路的頁面不正確 
  71. $post_data['formhash'] = $formhash
  72.  
  73.  
  74. $ch = curl_init($send_url); 
  75. curl_setopt($ch, CURLOPT_REFERER, $send_url);       //偽裝REFERER 
  76. curl_setopt($ch, CURLOPT_HEADER, 0); 
  77. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
  78. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); 
  79. curl_setopt($ch, CURLOPT_POST, 1); 
  80. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
  81. $contents = curl_exec($ch); 
  82. curl_close($ch); 
  83.  
  84. //清理cookie文件 
  85. unlink($cookie_file); 
  86. ?> 

CURL實現網站模擬登陸,代碼如下:

  1. <?php 
  2. $cookie_file=tempnam('./temp','cookie');$login_url='/bbs/logging.php?action=login&amp;loginsubmit=yes';$post_fields='username=用戶名&password=用戶密碼&referer=index.php&formhash=24eca8af&loginfield=username&questionid=0&loginsubmit=登錄';$ch = curl_init($login_url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$post_fields);curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);curl_exec($ch);curl_close($ch);$url='/bbs';$ch =curl_init($url);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file);$contents=curl_exec($ch);echo $contents;curl_close($ch); 
  3. ??> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资中县| 焉耆| 雅安市| 嘉兴市| 读书| 乌兰察布市| 得荣县| 临猗县| 塔河县| 江川县| 淮北市| 九江市| 昭苏县| 东乡县| 通化县| 固始县| 来宾市| 德庆县| 正镶白旗| 沿河| 晴隆县| 尼勒克县| 高唐县| 苍梧县| 杨浦区| 股票| 托克逊县| 德庆县| 桐柏县| 丁青县| 石家庄市| 襄汾县| 永宁县| 亳州市| 连平县| 双牌县| 凤翔县| 乐安县| 淮滨县| 富川| 肥西县|