Several APIs of Bangumi and simple tests using PHP calls
API used
For a complete API catalog, see: https://github.com/billgateshxk/bangumi
Log in (post)
URL: https://api.bgm.tv/auth?source= $appName PostData:username PostData:password Parameters: AppName: required; Username: required, email address; Password: required, password; Return value: Json (returns HTML when the user information does not exist), including personal information, auth, authEncode, etc. If there is an error, it will contain error information.
Get All Favorites
URL: https://api.bgm.tv/user/ $userId/collection? cat=$cat Parameters: $userId: required, user id; $cat: unknown, can be filled in blindly; Return value: Json array, which contains the collection episode object. If there is no collection content, the returned string is null.
Get the viewing details of a user's play
URL: https://api.bgm.tv/user/ $userId/progress? subject_id=$subjectId&source=$appName&auth=$authEncode Parameters: $userId: required, user id; $subjectId: required, play ID; $appName: required; $authEncode, required. Return value: Json, It contains the user's detailed information about the play (only the episodes that have been watched or operated). If there is no detailed information, null is returned.
Get the summary description of a play
URL: https://api.bgm.tv/subject/ $subjectId? responseGroup=simple Parameters: $subjectId: required, play ID; Return value: Json, It contains the summary information of the play. If there is no detailed information, it returns json code 404.
Get the detailed description of a play
URL: https://api.bgm.tv/subject/ $subjectId? responseGroup=large Parameters: $subjectId: required, play ID; Return value: Json, It contains the details of the play. If there is no details, it returns json code 404.
PHP test access code
Simple renderings
Complete code
<? php class BangumiAPI { /** OooOooOooO **/ private static $bangumiAPI = null; /**Static member**/ //Application Name private static $appName = "BGMYetu"; //Api link private static $apiUrl = " https://api.bgm.tv "; /**Members**/ //User name (email) public $userName = ""; //Password public $passWord = ""; //User ID private $userID = ""; //auth private $auth = ""; //auth urlencoding private $authEncode = ""; //Collection private $myCollection; //Login api private $loginApi = ""; //Collection api private $collectionApi = ""; /**Methods**/ //OooOooO public static function GetInstance() { if (BangumiAPI::$bangumiAPI == null) { BangumiAPI::$bangumiAPI = new BangumiAPI(); } return BangumiAPI::$bangumiAPI; } //Construction method private function __construct() { //Echo "Construction Method"; } //Object Property Initialization public function init($_userName,$_passWord) { if ($_userName == null || $_passWord == null) { //Program return Echo "Initialization parameter error! "; return; } $this->userName = $_userName; $this->passWord = $_passWord; //Login api $this->loginApi = BangumiAPI::$apiUrl . "/auth?source=" . BangumiAPI::$appName; //User ID is empty or auth is empty if ($this->userID == "" || $this->authEncode == ""){ //Login post string $postData = array('username' => $this->userName , 'password' => $this->passWord); //Get login and return to json $userContent = BangumiAPI::curl_post_contents($this->loginApi,$postData); //json to object $userData = json_decode($userContent); //Error attribute exists if (property_exists($userData, "error")) { //Output error message Echo "Login error:". $userData ->error; //Program return return; } //Initialization $this->userID = $userData->id; $this->auth = $userData ->auth; $this->authEncode = $userData ->auth_encode; } //Initialize Collection String $this->collectionApi = BangumiAPI::$apiUrl . "/user/" . $ this->userID ."/collection? cat=playing"; } //Get favorite json public function GetCollection() { if ($this->userID == "" || $this->collectionApi == "") { return null; } return BangumiAPI::curl_get_contents($this->collectionApi); } //Format Collection public function ParseCollection() { $content = $this->GetCollection(); if ($content == null || $content == "") { Echo "Failed to obtain"; return; } //The return is not json if (strpos($content, "[{") != false && $content != "") { Echo "The user does not exist! "; return; } $collData = json_decode($content); if (sizeof($collData) == 0 || $collData == null) { //Echo "No record yet"; return; } $index = 0; foreach ($collData as $value) { $name = $value->name; $name_cn = $value->subject->name_cn; $theurl = $value->subject->url; $img_grid =$value->subject->images->grid; $this->myCollection[$index++] = $value; } } //Get detailed progress public function GetProgress($_subjectID) { if ($this->authEncode == "" || $this->userID == "") { return null; } $progressApi = BangumiAPI::$apiUrl . "/user/" . $ this->userID . "/progress? subject_id=". $_ subjectID . "&source=" . self::$appName . "&auth=" . authEncode; $content = BangumiAPI::curl_get_contents($progressApi); //print_r($content); return $content; } public function ParseProgress($_subjectID) { $content = $this->GetProgress($_subjectID); //Not collecting or reading if ($content == "null") { return 0; } //I haven't seen it in my collection if ($content == "") { return 0; } $progressValue = json_decode($content); //Return to the episode to watch the detailed progress return $progressValue; } //Print Collection public function PrintCollecion($flag = true) { if ($this->myCollection == null) { $this->ParseCollection(); } switch ($flag) { case true: if (sizeof($this->myCollection) == 0 || $this->myCollection == null) { Echo "No record yet"; return; } echo " <style> div.bangumItem{ height:60px; line-height:20px; margin-bottom:5px; border:1px solid #ff8c83; max-width:280px; white-space:nowrap; } div.bangumItem img{ width:60px; height:auto; display:inline-block; float:left; padding-right:5px; } div.bangumItem a{ text-decoration:none; color:#ff8c83; } div.bangumItem p{ text-overflow:ellipsis;overflow:hidden; margin:0 auto auto 0; } div.bangumItem div.jinduBG{ position:relative; height:16px; width:195px; background-color:gray; display:inline-block; border-radius:4px; } div.bangumItem div.jinduFG { height:16px; background-color:#ff8c83; border-radius:4px; } div.bangumItem div.jinduText { position:absolute; width:100%;height:auto; z-index:99; text-align:center; color:#fff; line-height:15px; font-size:15px; } </style> "; foreach ($this->myCollection as $value) { //print_r($value); //$id = $value->subject->id; $epsNum = $value->subject->eps; $progressNum = $value->ep_status; $myProgress = $progressNum . "/" . $ epsNum; $name = $value->name; $name_cn = $value->subject->name_cn; $theurl = $value->subject->url; $img_grid =$value->subject->images->grid; $progressWidth = $progressNum / $epsNum * 195; echo " <div class = 'bangumItem'> <a href=" . $theurl ." target='_blank' rel="noopener noreferrer"> <img src='$img_grid' /> <p>$name<br> $name_cn<br> <div class='jinduBG'> <div class='jinduText '>Progress: $myProgress</div> <div class='jinduFG' style='width:" . $progressWidth . "px;'> </div> </div> </p> </a> </div>"; } break; case false: echo $myCollection; break; default: break; } } //Get Get Content private static function curl_get_contents($_url) { echo "The GET Url You Request is <span style='color:#ff8c83'>" . $_url . "</span><br/>"; $myCurl = curl_init($_url); //Do not verify certificate curl_setopt($myCurl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($myCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($myCurl, CURLOPT_RETURNTRANSFER, true); curl_setopt($myCurl, CURLOPT_HEADER, false); //Get $content = curl_exec($myCurl); //Close curl_close($myCurl); return $content; } //Get content by post private static function curl_post_contents($_url,$_postdata) { echo "The POST Url You Request is <span style='color:#ff8c83'>" . $_url . "</span><br/>"; $myCurl = curl_init($_url); //Do not verify certificate curl_setopt($myCurl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($myCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($myCurl, CURLOPT_RETURNTRANSFER, true); curl_setopt($myCurl, CURLOPT_POST, 1); curl_setopt($myCurl, CURLOPT_POSTFIELDS, $_postdata); $output = curl_exec($myCurl); curl_close($myCurl); return $output; } } $bangum = BangumiAPI::GetInstance(); $bangum->init(" you@yourmail.com ","123465789"); $bangum->ParseCollection(); $bangum->PrintCollecion(true); ?>
References
Comment
Comment area
0F 0F 0F A very good idea 0F Calling the api is to splice the get request and then the server returns the data in json format, which is very convenient to obtain the data 0F Very good. It's installed 0F ` https://api.bgm.tv/user/ $userId/progress? Subject_id=$subjectId&source=$authEncode ` Why do I always use this interface as an Unauthorized QAQ 0F Mm-hmm. That's OK. Add a useful interface~ “` https://api.bgm.tv/subject/ $subjectId? responseGroup=simple #Get the brief information of Fan Opera https://api.bgm.tv/subject/ $subjectId? responseGroup=large #Get the details of Fancy Opera “` 0F I beg your pardon? Station A is cold? 0F sure 0F Hello author, after I filled in the account password, I used the third-party api in the plug-in, but failed to write a code to obtain it. Do I need to set something else? 0F Thank you very much.