PHP get cookie, Token, login simulation, data capture, parse generate json

This article describes the use of PHP acquisition cookie, get Token, as well as simulation log on, then grab the data, and finally generate json parsing process is.

 

0. Set Cookie Path

set_time_limit(0);

//使用的cookie路径,
if (isset($_SERVER['HTTP_APPNAME'])){
    $cookie = SAE_TMP_PATH."/cookie.txt";
}else {
    $cookie = dirname(__FILE__)."/cookie.txt";
}

 

1. Open the page for COOKIEJAR, and token, and save

$url = "http://www.fangbei.org/#agent/login";
$headers = array(
"User-Agent: 来源 方倍工作室 www.fangbei.org",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //首次只接收
$result = curl_exec($curl);
curl_close($curl);

$pattern = '/name="_token" value="(.*?)"/is';
preg_match_all($pattern, $result, $matches);
if (isset($matches[1][0])){
    $token= $ The matches [. 1] [0 ]; 
} the else {
     Die ( "Get token failed" ); 
}

 

2, Log

 #2. 登录
$url = "http://www.fangbei.org/#/agent/login";
$headers = array(
"User-Agent: 来源 方倍工作室 www.fangbei.org",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Origin: http://www.fangbei.org/#",
"Referer: http://www.fangbei.org/#/agent/login",
);

$fields = '_token='.$token.'&username=fangbei&password=fangbei.org';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);    //发送cookie
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);     //接收 cookie
curl_exec($curl);
curl_close($curl);

 

3. Take Data

$url = "http://www.fangbei.org/#agent/AgentProductLink";
$headers = array(
"User-Agent: 来源 方倍工作室 www.fangbei.org",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Referer: http://www.fangbei.org/#agent/welcome",
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);    //发送cookie
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);     //接收 cookie
$content = curl_exec($curl);
curl_close($curl);

 

4, parsing the data to generate json

require_once('simple_html_dom.php');
// var_dump($content);
$html_main = str_get_html($content);
if (!isset($html_main)){
    $html_main->clear();
    die("页面载入出错!");
}
$tjarray = array();
foreach($html_main->find('tr[class="cot"]') as $item)
{
    $id = @$item->find('td', 0)->plaintext;
    $title = @$item->find('td', 1)->plaintext;
    $button = @$item->find('div[class="btn btn-fill"]', 0)->outertext;
    $pattern = "/\(\'(.+?)\'\)/"; // copyUrl('http://www.fangbei.org/#top-apply/Affection-28679')
    preg_match_all($pattern, $button, $matches);
    $bturl = $matches[1][0];
    $tjarray[$id] = array("title"=>urlencode($title),"url"=>urlencode($bturl));
    // break;
}
$html_main->clear();
echo urldecode(json_encode($tjarray));

 

Guess you like

Origin www.cnblogs.com/txw1958/p/php-login-simulation.html