64 lines
1.9 KiB
PHP
64 lines
1.9 KiB
PHP
<?php
|
|
function getLeetCodeProfile($handle) {
|
|
$url = 'https://leetcode.com/graphql';
|
|
$query = '
|
|
query getUserProfile($username: String!) {
|
|
matchedUser(username: $username) {
|
|
username
|
|
submitStats: submitStatsGlobal {
|
|
acSubmissionNum {
|
|
difficulty
|
|
count
|
|
submissions
|
|
}
|
|
}
|
|
profile {
|
|
ranking
|
|
userAvatar
|
|
realName
|
|
}
|
|
languageProblemCount {
|
|
languageName
|
|
problemsSolved
|
|
}
|
|
recentAcSubmissionList {
|
|
id
|
|
title
|
|
titleSlug
|
|
timestamp
|
|
}
|
|
}
|
|
}
|
|
';
|
|
|
|
$variables = ['username' => $handle];
|
|
$data = ['query' => $query, 'variables' => $variables];
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
'Content-Length: ' . strlen(json_encode($data))
|
|
]);
|
|
|
|
$result = curl_exec($ch);
|
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
|
|
if ($result === FALSE || $httpcode != 200) {
|
|
return ['error' => 'Failed to fetch data from LeetCode.'];
|
|
}
|
|
|
|
$profileData = json_decode($result, true);
|
|
|
|
if (isset($profileData['errors']) || !isset($profileData['data']['matchedUser']) || $profileData['data']['matchedUser'] === null) {
|
|
return ['error' => 'User not found or API error.'];
|
|
}
|
|
|
|
return $profileData['data']['matchedUser'];
|
|
}
|
|
?>
|