Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 KB | None | 0 0
  1. <?php
  2. class Mal_API {
  3.   private $username;
  4.   private $password;
  5.   private $user_id;
  6.  
  7.   private $curl;
  8.   private $queryurl;
  9.   function __construct()
  10. {
  11.     $a = func_get_args();
  12.     $i = func_num_args();
  13.     if($i!=2)
  14.     {
  15.       throw "Please enter a username and password";
  16.     }
  17.     $this->username=$a[0];
  18.     $this->password=$a[1];
  19.  
  20.  
  21.     $this->curl = curl_init();
  22.     curl_setopt($this->curl, CURLOPT_USERPWD, $this->username.":".$this->password);
  23.     curl_setopt($this->curl,CURLOPT_RETURNTRANSFER,true);
  24.     curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // setting this to false so SSL stuff doesn't bug you
  25.     curl_setopt($this->curl, CURLOPT_VERBOSE, true);
  26.     curl_setopt($this->curl,CURLOPT_CONNECTTIMEOUT ,1000);
  27.     curl_setopt($this->curl,CURLOPT_TIMEOUT ,1000);
  28.     curl_setopt($this->curl,CURLOPT_MAXREDIRS,10);
  29.     curl_setopt($this->curl, CURLOPT_ENCODING ,"");
  30.     $this->queryurl="https://myanimelist.net/api/account/verify_credentials.xml";
  31.     $res=$this->send_query();
  32.     if($res==-1)
  33.     {
  34.       throw "error Wrong Credidentials";
  35.     }
  36.     else {
  37.       $user_id= $res->id;
  38.     }
  39.  
  40. }
  41.  
  42.  
  43. public function search_for_anime($title)
  44. {
  45.   echo $title;
  46.   $title=curl_escape($this->curl,$title);
  47.    $this->queryurl='https://myanimelist.net/api/anime/search.xml?q='.$title; //todo maybe lowercase everything don't know how this would work with japanese titles
  48.    echo $this->queryurl;
  49.    return $this->send_query();
  50. }
  51. private function send_query() {
  52.          curl_setopt($this->curl,CURLOPT_URL,$this->queryurl);
  53.          $result = curl_exec($this->curl);
  54.          $last = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
  55.          $status = curl_getinfo($this->curl,CURLINFO_HTTP_CODE);
  56.          $xml = new SimpleXMLElement($result);
  57.         if($status==200) //todo add message check
  58.          {
  59.           // echo $result;
  60.            return $xml;
  61.          }
  62.          else {
  63.            echo "Status:".$status;
  64.            return -1;
  65.          }
  66.  
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement