Advertisement
Guest User

WykopAPI LIB

a guest
Mar 23rd, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Wykop API
  4.  * Biblioteka służy do obsługi API serwisu wykop.pl
  5.  *
  6.  * @author: maciejkiner
  7.  * @version: 1.0
  8.  */
  9. class libs_Wapi
  10. {
  11.     protected $userAgent = 'WykopAPI';
  12.     protected $apiDomain = 'http://a.wykop.pl/';
  13.     protected $key = null;
  14.     protected $secret = null;
  15.     protected $userKey = null;
  16.     protected $format = 'json';
  17.     protected $output = 'clear';
  18.     protected $isValid;
  19.     protected $error;
  20.        
  21.     /*
  22.      * Kontruktor
  23.      *
  24.      * @param string $key - appkey
  25.      * @param string $secret - appsecret
  26.      * @param string $output - output format
  27.      *
  28.      */
  29.     public function __construct($key, $secret, $output = null)
  30.     {
  31.         $this->key = $key;
  32.         $this->secret = $secret;
  33.         if ($output !== null)
  34.         {
  35.             $this->output = $output;
  36.         }
  37.     }
  38.    
  39.     /*
  40.      * Wykonanie requestu do API
  41.      *
  42.      * @param string $action - zasób, np. "links/upcoming"
  43.      * @param array $postData - post
  44.      * @param array $filesData - pliki, np array('embed' => "@plik.jpg;type=image/jpeg")
  45.      *
  46.      * @return array - odpowiedź API
  47.      */
  48.     public function doRequest($action, $postData = null, $filesData = null)
  49.     {
  50.         $url = $this->apiDomain . $action .= (strpos($action, ',') ? ',' : '/') . $this->getKey() . $this->getFormat() . $this->getOutput() . $this->getUserKey();
  51.         if ($postData === null)
  52.         {
  53.             $result = $this->curl($url);
  54.         }
  55.         else
  56.         {
  57.             if ($filesData !== null)
  58.             {
  59.                 $postData = $filesData + $postData;
  60.             }
  61.             $result = $this->curl($url, $postData);
  62.         }
  63.         $this->checkIsValid($result);
  64.         return $this->isValid ? json_decode($result['content'], true) : array();
  65.     }
  66.  
  67.     /*
  68.      * Czy zapytanie było poprawne
  69.      *
  70.      * @return bool - poprawna odpowiedź
  71.      */
  72.     public function isValid()
  73.     {
  74.         return $this->isValid;
  75.     }
  76.    
  77.     /*
  78.      * Błąd ostatniego zapytania
  79.      *
  80.      * @return string - komunikat błędu
  81.      */
  82.     public function getError()
  83.     {
  84.         return $this->error;
  85.     }
  86.    
  87.     protected function checkIsValid(&$result)
  88.     {
  89.         $this->error = null;
  90.         if (empty($result['content']))
  91.         {
  92.             $this->isValid = false;
  93.         } else {
  94.             $json = json_decode($result['content'], true);
  95.             if (!empty($json['error']))
  96.             {
  97.                 $this->isValid = false;
  98.                 $this->error = $json['error']['message'];
  99.             } else {
  100.                 $this->isValid = true;
  101.             }
  102.         }
  103.     }
  104.    
  105.     protected function setUserKey($userKey)
  106.     {
  107.         $this->userKey = $userKey;
  108.     }
  109.    
  110.     protected function getUserKey()
  111.     {
  112.         if (!empty($this->userKey))
  113.         {
  114.             return 'userkey/' . $this->userKey . '/';
  115.         }
  116.     }
  117.    
  118.     protected function getFormat()
  119.     {
  120.         if (!empty($this->format))
  121.         {
  122.             return 'format/' . $this->format . '/';
  123.         }
  124.     }
  125.    
  126.     protected function getOutput()
  127.     {
  128.         if (!empty($this->output))
  129.         {
  130.             return 'output/' . $this->output . '/';
  131.         }
  132.     }
  133.    
  134.     protected function getKey()
  135.     {
  136.         if (!empty($this->key))
  137.         {
  138.             return 'appkey/' . $this->key . '/';
  139.         }
  140.     }  
  141.    
  142.     protected function sign($url, $post = null)
  143.     {
  144.         if ($post !== null)
  145.         {
  146.             ksort($post);
  147.         }
  148.         return md5($this->secret . $url . ($post === null ? '' : implode(',', $post)));
  149.     }
  150.    
  151.     protected function curl($url, $post = null)
  152.     {
  153.         $options = array(
  154.                 CURLOPT_RETURNTRANSFER  => true,
  155.                 CURLOPT_HEADER          => false,
  156.                 CURLOPT_ENCODING        => '',
  157.                 CURLOPT_USERAGENT       => $this->userAgent,
  158.                 CURLOPT_AUTOREFERER     => true,
  159.                 CURLOPT_CONNECTTIMEOUT  => 15,
  160.                 CURLOPT_TIMEOUT         => 15,
  161.                 CURLOPT_MAXREDIRS       => 10,
  162.                 CURLOPT_HTTPHEADER      => array('apisign:' . $this->sign($url, $post))
  163.         );
  164.          
  165.         if ($post !== null)
  166.         {
  167.             $post_value = is_array($post) ? http_build_query($post, 'f_' , '&') : '';
  168.             $options[CURLOPT_POST] = 1;
  169.             $options[CURLOPT_POSTFIELDS] = $post;
  170.         }
  171.  
  172.         $ch  = curl_init($url);
  173.         curl_setopt_array($ch, $options);
  174.         $content = curl_exec($ch);
  175.         $err = curl_errno($ch);
  176.         $errmsg = curl_error($ch);
  177.         $result = curl_getinfo($ch);
  178.         curl_close($ch);
  179.    
  180.         $result['errno'] = $err;
  181.         $result['errmsg'] = $errmsg;
  182.         $result['content'] = $content;       
  183.         return $result;
  184.     }
  185.    
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement