php爬蟲(curl) 模擬登錄獲取cookie,而後使用cookie查看我的中心

<!-- curl的高級使用 -->
<?php 
//模擬登錄獲取cookie保存到電腦
header("content-Type: text/html; charset=UTF-8");
/*$cookie_file = tempnam('d:/', 'cookie');*/
$cookie_file = 'd:/demo.txt';
$login_url="http://www.php-z.com/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LJ0CC&inajax=1";
/*$post_fields="ormhash=354c7225&referer=http://www.php-z.com/&loginfield=username&username=chenglin1994&password=chenglin1994&questionid=0&answer=";*/
$post_fields =array('ormhash' => '354c7225', 
                    'referer' => 'http://www.php-z.com/', 
                    'loginfield' => 'username', 
                    'username' => 'chenglin1994', 
                    'password' => 'chenglin1994',
                    'questionid' => '0',
                    'answer' => ''
);

//提交登陸表單請求
$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); //存儲提交後獲得的cookie數據
echo curl_exec($ch);
curl_close($ch);



//利用cookie查看我的中心
$url = 'http://www.php-z.com/home.php?mod=space&do=friend';
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file); //使用提交後獲得的cookie數據作參數
$contents=curl_exec($ch);
curl_close($ch);
echo $contents;

 

關於curl的優秀文章php

php curl cookie 存取示例(標準採集程序)及假裝

https://blog.csdn.net/e421083458/article/details/19326893html

 

PHP cURL應用實現模擬登陸與採集使用方法詳解

http://www.cnblogs.com/Zjmainstay/p/php-curl.htmlajax

相關文章
相關標籤/搜索