Skip to content

Instantly share code, notes, and snippets.

@manhdoan1392
Created May 30, 2020 04:28
Show Gist options
  • Save manhdoan1392/a16e2e75042335778e151c8ab545210a to your computer and use it in GitHub Desktop.
Save manhdoan1392/a16e2e75042335778e151c8ab545210a to your computer and use it in GitHub Desktop.
<?php
$response = LoginShopee('username','password');
echo $response;
function LoginShopee($SP_Username,$SP_Pass) {
//Khởi tạo chung cho toàn bộ request bên dưới
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0");
$cookie_jar = 'cookie_shopee.txt';
curl_file_create($cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
// Request lấy cookie ban đầu
curl_setopt($ch, CURLOPT_URL, "https://shopee.vn/api/v0/buyer/login/");
curl_exec($ch);
// echo file_get_contents($cookie_jar) .'<br><br>';
//Khởi tạo các dữ liệu đầu vào cho request Login
$csrf_token = csrftoken();
$header = array(
'x-csrftoken: '. $csrf_token,
'x-requested-with: XMLHttpRequest',
'referer: https://shopee.vn/api/v0/buyer/login/',
);
$data = array(
"login_key" => $SP_Username,
"login_type" => "username",
"password_hash" => CryptPass($SP_Pass),
"captcha" => "",
"remember_me" => "true"
);
$data = http_build_query($data);
//Request login
curl_setopt($ch, CURLOPT_URL, "https://shopee.vn/api/v0/buyer/login/login_post/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_COOKIE, "csrftoken=" . $csrf_token);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDSIZE, strlen($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
echo $response;
// Request lấy thông tin tài khoản sau khi đã đăng nhập thành công
curl_setopt($ch, CURLOPT_URL, "https://banhang.shopee.vn/api/v1/login/");
$response = curl_exec($ch);
return $response;
curl_close($ch);
}
function CryptPass($pass){
return hash("sha256", md5($pass));
}
function csrftoken() {
$karakter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$PanjangKarakter = strlen($karakter);
$acakString = '';
for ($i = 0; $i < 32; $i++) {
$acakString .= $karakter[rand(0, $PanjangKarakter - 1)];
}
return $acakString;
}
?>
@Ngovanthuan989
Copy link

Chào bạn
Bên shopee khi login còn bước xác thực OTP nữa thì mới lấy được cookies
Bạn có thể giúp mình phần này được không ạ

@puzzle9
Copy link

puzzle9 commented May 27, 2022

wow thinks

@ChiQuang98
Copy link

Bạn ơi giờ có api mới login rồi, mình chưa tìm được. bạn có cách nào tìm ra api mới đó không ạ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment