Several APIs of Bangumi and simple tests using PHP calls

Bangumi Fanzuo Plan is an ACGN network that focuses on animation, music and games to help you share, discover and get to know your peers.

A few days ago, I saw Bangumi based drama tracking directory function in some charging topics, which was very interesting. However, Bangumi does not disclose the API interface, and I have no channel to create a charging theme with this function. Fortunately, I found the Bangumi API in the IOS version of BangumiAPP developed by billgateshxk on GITHUB. Later, I wrote a very simple PHP code to call it.

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

I don't know PHP. I read the PHP manual while writing. I feel that all kinds of symbols are troublesome (C # Dafa is good).

 <? 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

1. [Caption] [Bangumi] Bangumi Bangumi404 small TV logo
2. [Information] [GITHUB] billgateshxk bangumi

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/2768.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. Sign in successfully! Sign in time: 11:36:49 p.m., clock in every day, and life will be more exciting~

  2. Sign in successfully! Sign in time: 7:55:41 p.m., clock in every day, and life will be more exciting~

  3. at leisure 11-24 23:55 reply

    A very good idea

  4. answer 11-27 11:41 reply

    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 :mrgreen:

  5. Mashiro 02-01 00:02 reply

    Very good. It's installed

  6. Mashiro 02-01 20:03 reply

    ` https://api.bgm.tv/user/ $userId/progress? Subject_id=$subjectId&source=$authEncode ` Why do I always use this interface as an Unauthorized QAQ

    • hare 02-02 00:01 reply

      No, it seems that something is wrong. I'll find the reason.

    • hare 02-02 00:21 reply

      The reason is found. There is one missing API parameter and one wrong parameter value in the article. The corresponding API link in the article has been modified and the test has passed.

  7. Mashiro 02-02 10:11 reply

    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
    “`

  8. c0smxsec 02-02 23:42 reply

    I beg your pardon? Station A is cold?

  9. mikusa 03-01 00:18 reply

    sure

  10. Yuanshuang 05-30 21:00 reply

    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?

  11. Spirit 10-04 10:24 reply

    Thank you very much.