代码如下
<?php
namespace app\core;
use Curl\Curl;
class ZfbTk
{
private static $cookie_file = app.'/data/cookie.txt';
private static $cookie_str_file = app.'/data/cookie_str.txt';
public static function upTk()
{
self::account();
// die;
self::scheck();
self::lcheck();
}
public static function getBalance()
{
$cookie_arr = self::getTk();
$start_time = urlencode(date('Y-m-d H:i:s',strtotime(date('Y-m-d'))));
$end_time = urlencode(date('Y-m-d H:i:s',strtotime(date('Y-m-d'))+24*60*60));
$url = 'https://mbillexprod.alipay.com/enterprise/accountTotalAssetQuery.json?ctoken='.$cookie_arr['ctoken'].'&_output_charset=utf-8';
$post_data = 'billUserId='.$cookie_arr['CLUB_ALIPAY_COM'].'&pageNum=1&pageSize=20&startTime='.$start_time.'&endTime='.$end_time.'&status=ALL&sortType=0&_input_charset=gbk';
$headers = [];
$headers_str = 'Accept:
application/json
Accept-Encoding:
gzip, deflate, br, zstd
Accept-Language:
zh-CN,zh;q=0.9
Content-Length:
156
Content-Type:
application/x-www-form-urlencoded; charset=UTF-8
Origin:
https://b.alipay.com
Referer:
https://b.alipay.com/
Sec-Ch-Ua:
"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
Sec-Ch-Ua-Mobile:
?0
Sec-Ch-Ua-Platform:
"Windows"
Sec-Cookie-Deprecation:
label_only_3
Sec-Fetch-Dest:
empty
Sec-Fetch-Mode:
cors
Sec-Fetch-Site:
same-site
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36';
$headers = self::headerStrToArr($headers_str);
$curl = new Curl();
$curl->setCookies($cookie_arr);
$curl->setHeaders($headers);
$curl->post($url,$post_data);
if($curl->error){
die($curl->errorMessage);
}
$res = $curl->response;
$res = json_decode($res,true);
if($res['status']!=='succeed')
die('error');
$blance = floatval($res['result']['availableBalance']);
// echo '余额:'.$blance;
return $blance;
}
private static function account()
{
if(!is_file(self::$cookie_file)){
self::writeCookieArray([]);
}
$cookie_arr = self::readCookieArray();
if(empty($cookie_arr)){
$cookie_arr = self::cookieStrToArr(self::readCookieStr());
}
$url = 'https://enterpriseportal.alipay.com/pamir/login/queryLoginAccount.json?_output_charset=utf-8&appScene=MRCH';
$headers_str = 'Accept:
*/*
Accept-Encoding:
gzip, deflate, br, zstd
Accept-Language:
zh-CN,zh;q=0.9
Origin:
https://b.alipay.com
Referer:
https://b.alipay.com/
Sec-Ch-Ua:
"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
Sec-Ch-Ua-Mobile:
?0
Sec-Ch-Ua-Platform:
"Windows"
Sec-Cookie-Deprecation:
label_only_3
Sec-Fetch-Dest:
empty
Sec-Fetch-Mode:
cors
Sec-Fetch-Site:
same-site
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36';
$headers = self::headerStrToArr($headers_str);
/*dump($url);
dump($headers);
dump($cookie_arr);
dump($url);
die;*/
$curl = new Curl();
$curl->setCookies($cookie_arr);
$curl->setHeaders($headers);
$curl->get($url);
if($curl->error){
die($curl->errorMessage);
}
$res = $curl->response;
if($res->stat!=='ok')
die('error');
$net_cookies = $curl->responseCookies;
if(!empty($net_cookies)){
foreach ($net_cookies as $key=>$item){
$cookie_arr[$key] = $item;
}
self::writeCookieArray($cookie_arr);
}
echo date('Y-m-d H:i:s').' ';
echo $res->data->logonId;
echo '->';
echo json_encode($net_cookies,JSON_UNESCAPED_UNICODE);
}
private static function scheck()
{
$cookie_arr = self::readCookieArray();
$url = 'https://openhome.alipay.com/home/sessionCheck.json?ctoken='.$cookie_arr['ctoken'];
$headers = [];
$headers_str = 'Accept:
application/json, text/plain, */*
Accept-Encoding:
gzip, deflate, br, zstd
Accept-Language:
zh-CN,zh;q=0.9
Origin:
https://b.alipay.com
Referer:
https://b.alipay.com/
Sec-Ch-Ua:
"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
Sec-Ch-Ua-Mobile:
?0
Sec-Ch-Ua-Platform:
"Windows"
Sec-Cookie-Deprecation:
label_only_3
Sec-Fetch-Dest:
empty
Sec-Fetch-Mode:
cors
Sec-Fetch-Site:
same-site
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36';
$headers = self::headerStrToArr($headers_str);
$curl = new Curl();
$curl->setCookies($cookie_arr);
$curl->setHeaders($headers);
$curl->get($url);
if($curl->error){
die($curl->errorMessage);
}
$res = $curl->response;
if($res->stat!=='ok')
die('scheck error');
$net_cookies = $curl->responseCookies;
if(!empty($net_cookies)){
foreach ($net_cookies as $key=>$item){
$cookie_arr[$key] = $item;
}
self::writeCookieArray($cookie_arr);
}
echo ' scheck ok ';
echo json_encode($net_cookies,JSON_UNESCAPED_UNICODE);
echo "\n";
}
private static function lcheck()
{
$cookie_arr = self::readCookieArray();
$url = 'https://auth.alipay.com/login/loginSessionRenewal.do?ctoken='.$cookie_arr['ctoken'];
$headers = [];
$headers_str = 'Accept:
application/json, text/plain, */*
Accept-Encoding:
gzip, deflate, br, zstd
Accept-Language:
zh-CN,zh;q=0.9
Content-Length:
0
Origin:
https://b.alipay.com
Referer:
https://b.alipay.com/
Sec-Ch-Ua:
"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
Sec-Ch-Ua-Mobile:
?0
Sec-Ch-Ua-Platform:
"Windows"
Sec-Cookie-Deprecation:
label_only_3
Sec-Fetch-Dest:
empty
Sec-Fetch-Mode:
cors
Sec-Fetch-Site:
same-site
User-Agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36';
$headers = self::headerStrToArr($headers_str);
$curl = new Curl();
$curl->setCookies($cookie_arr);
$curl->setHeaders($headers);
$curl->post($url);
if($curl->error){
die($curl->errorMessage);
}
$res = $curl->response;
$res = json_decode($res);
if($res->success!==true)
die('lcheck error');
$net_cookies = $curl->responseCookies;
if(!empty($net_cookies)){
foreach ($net_cookies as $key=>$item){
$cookie_arr[$key] = $item;
}
self::writeCookieArray($cookie_arr);
}
echo ' lcheck ok ';
echo json_encode($net_cookies,JSON_UNESCAPED_UNICODE);
echo "\n";
}
private static function readCookieArray(){
$cookie_arr = unserialize(openssl_decode(file_get_contents(self::$cookie_file)));
return $cookie_arr;
}
private static function writeCookieArray($arr){
file_put_contents(self::$cookie_file,openssl_encode(serialize($arr)));
}
private static function readCookieStr(){
$str = openssl_decode(file_get_contents(self::$cookie_str_file));
return $str;
}
private static function writeCookieStr($str){
file_put_contents(self::$cookie_str_file,openssl_encode($str));
}
private static function cookieStrToArr($str)
{
$cookie_str = preg_replace('/@[\s\S]+(; |$)/U',"",$str);
$cookie_str_tmp = explode('; ',$cookie_str);
$cookie_str_tmp = array_filter($cookie_str_tmp);
$cookie_arr = [];
foreach ($cookie_str_tmp as $item){
if(strpos($item,'="')!==false){
$tmpx = preg_replace('/"$/','',$item);
$tmpxx = explode('="',$tmpx);
$cookie_arr[$tmpxx[0]]=$tmpxx[1];
}else{
$tmpxx = explode('=',$item);
$cookie_arr[$tmpxx[0]]=$tmpxx[1];
}
}
return $cookie_arr;
}
private static function readNetCookieArray(){
$url = 'http://movie.etafort.cn/data/cookie.txt';
return unserialize(openssl_decode(file_get_contents($url)));
}
private static function headerStrToArr($str)
{
$headers_str = preg_replace('/:\n/m',': ',$str);
$headers_str_arr = explode("\n",$headers_str);
$arr = [];
foreach ($headers_str_arr as $item){
$tmp = explode(': ',$item);
$arr[$tmp[0]] = $tmp[1];
}
return $arr;
}
private static function getTk()
{
// return self::readNetCookieArray();
return self::readCookieArray();
}
}