<?php
//模擬登錄
function login_post($url, $cookie, $post) {
$curl = curl_init();//初始化curl模塊
curl_setopt($curl, CURLOPT_URL, $url);//登錄提交的地址
curl_setopt($curl, CURLOPT_HEADER, 0);//是否顯示頭信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自動(dòng)顯示返回的信息
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //設(shè)置Cookie信息保存在指定的文件中
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息
curl_exec($curl);//執(zhí)行cURL
curl_close($curl);//關(guān)閉cURL資源,并且釋放系統(tǒng)資源
}
// 登錄成功后獲取數(shù)據(jù)
function get_content($url, $cookie) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //讀取cookie
$rs = curl_exec($ch); //執(zhí)行cURL抓取頁面內(nèi)容
curl_close($ch);
return $rs;
}
// 登錄成功后模擬發(fā)帖
function post_thread($url, $cookie, $post)
{
$curl = curl_init();//初始化curl模塊
curl_setopt($curl, CURLOPT_URL, $url);//登錄提交的地址
curl_setopt($curl, CURLOPT_HEADER, 0);//是否顯示頭信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自動(dòng)顯示返回的信息
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); //讀取cookie
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息
curl_exec($curl);//執(zhí)行cURL
curl_close($curl);//關(guān)閉cURL資源烦秩,并且釋放系統(tǒng)資源
}
//設(shè)置post的數(shù)據(jù)
$post = array (
'user_id' => '123456@qq.com',
'password' => '123456',
'goto_page' => 'http://m.app.cn/index.php',
'act' => 'login',
't' => time(),
);
//登錄地址
$url = "http://m.app.cn/account/login.php";
//設(shè)置cookie保存路徑
$cookie = dirname(__FILE__) . '/cookie_curl.txt';
//登錄后要獲取信息的地址
$url2 = "http://m.app.cn/user/wap/my_index.php";
// 1.模擬登錄
login_post($url, $cookie, $post);
// 2.獲取登錄頁的信息
// $content = get_content($url2, $cookie);
//匹配頁面信息
// $preg = "/<td class='portrait'>(.*)<\/td>/i";
// preg_match_all($preg, $content, $arr);
// $str = $arr[1][0];
//輸出內(nèi)容
// echo $content;
// 3.模擬發(fā)帖
$thread_info = array(
'action' => 'pub',
'title' => 'Test curl',
'content' => 'Hello, world.',
't' => time(),
);
$pub_thread_url = 'http://m.app.cn/thread/api/pub_thread.php';
$ret = post_thread($pub_thread_url, $cookie, $thread_info);
print_r($ret);
//刪除cookie文件
@ unlink($cookie);
?>