daily pastebin goal
77%
SHARE
TWEET

Untitled

a guest Dec 18th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     class Api{
  3.         private function apiGet($url){
  4.         $oauth = new OAuth(getenv('CONSUMER_KEY'),getenv('CONSUMER_SECRET'));
  5.         $oauth->disableSSLChecks();
  6.         $oauth->setToken(getenv('ACCESS_TOKEN'),getenv('TOKEN_SECRET'));
  7.         $oauth->fetch($url);
  8.         return json_decode($oauth->getLastResponse(),true);
  9.         }
  10.  
  11.         private function buildQueryString($request){
  12.             if(!isset($request['length'])){
  13.                 $request['length'] = 8;
  14.             }
  15.             $request_string = http_build_query($request);
  16.             return $request_string;
  17.         }
  18.  
  19.     public function curl($url, $data=array(), $header = ""){
  20.       $curl = curl_init();
  21.       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  22.       curl_setopt_array($curl, array(
  23.         CURLOPT_RETURNTRANSFER => 1,
  24.         CURLOPT_URL => $url,
  25.         CURLOPT_POST => 1,
  26.         CURLOPT_POSTFIELDS => $data
  27.       ));
  28.       if($header=="json"){
  29.         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  30.       }
  31.       else if($header=="x-www-form-urlencoded"){
  32.         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  33.         curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  34.       }
  35.       $resp = curl_exec($curl);
  36.       if(!$resp){ return curl_error($curl); }
  37.       else{ return $resp;}
  38.       curl_close($curl);
  39.     }
  40.     function postData($url, $token,$data=null, $method = "post"){
  41.       $oauth = new OAuth(getenv('CONSUMER_KEY'),getenv('CONSUMER_SECRET'));
  42.       $oauth->disableSSLChecks();
  43.       $oauth->setToken($token['key'],$token['secret']);
  44.       if($method=="post"){ $http_method = OAUTH_HTTP_METHOD_POST; }
  45.       else if($method=="put"){ $http_method = OAUTH_HTTP_METHOD_PUT; }
  46.       else if($method=="delete"){ $http_method = OAUTH_HTTP_METHOD_DELETE; }
  47.  
  48.       if(isset($data['@attachment'])){
  49.         $oauth->setAuthType(OAUTH_AUTH_TYPE_NONE);
  50.         $oauth->setRequestEngine( OAUTH_REQENGINE_CURL );
  51.         $abs = array();
  52.         foreach($data as $key =>$value){
  53.           if(substr($value, 0,1)!=='@'){
  54.             $abs[$key] = $value;
  55.           }
  56.         }
  57.         $header = $oauth->getRequestHeader($http_method,$url,$abs);
  58.         $headers = array('Authorization'=>$header);
  59.         $oauth->fetch($url, $data, $http_method,$headers);
  60.       }
  61.       else if(isset($data['attachment'])){
  62.         $oauth->fetch($url, $data, $http_method, array("Content-Type"=>"application/x-www-form-urlencoded"));
  63.       }
  64.       else{
  65.         $oauth->fetch($url, $data, $http_method);
  66.       }
  67.       return json_decode($oauth->getLastResponse(),true);
  68.     }
  69.  
  70.     public function getToken(){
  71.       $oauth = new OAuth(getenv('CONSUMER_KEY'),getenv('CONSUMER_SECRET'));
  72.       $oauth->disableSSLChecks();
  73.       $request_token_info = $oauth->getRequestToken("https://oauth.8villages.com/tokens/request-token","oob",OAUTH_HTTP_METHOD_POST);
  74.       if(!empty($request_token_info)) {
  75.         return $request_token_info;
  76.       } else {
  77.         return "Failed fetching request token, response was: " . $oauth->getLastResponse();
  78.       }
  79.     }
  80.  
  81.         public function getArticles($request){
  82.             $request_string = $this->buildQueryString($request);
  83.             $url = 'https://conversation.8villages.com/1.0/contents/articles?'.$request_string;
  84.             return $this->apiGet($url);
  85.         }
  86.  
  87.     public function getQuestions($request){
  88.       $request_string = $this->buildQueryString($request);
  89.       $url = 'https://conversation.8villages.com/1.0/contents/questions?'.$request_string;
  90.       return $this->apiGet($url);
  91.     }
  92.  
  93.     public function getForums($request){
  94.       $request_string = $this->buildQueryString($request);
  95.       $url = 'https://conversation.8villages.com/1.0/contents/forums?'.$request_string;
  96.       return $this->apiGet($url);
  97.     }
  98.  
  99.     public function getStores($request){
  100.       $request_string = $this->buildQueryString($request);
  101.       $url = 'https://conversation.8villages.com/1.0/contents/storeItems?'.$request_string;
  102.       return $this->apiGet($url);
  103.     }
  104.  
  105.     public function getVideos($request){
  106.       $request_string = $this->buildQueryString($request);
  107.       $url = 'https://conversation.8villages.com/1.0/contents/videos?'.$request_string;
  108.       return $this->apiGet($url);
  109.     }
  110.  
  111.     public function getCalendar($request){
  112.       $request_string = $this->buildQueryString($request);
  113.       $url = 'https://conversation.8villages.com/1.0/contents/events?'.$request_string;
  114.       return $this->apiGet($url);
  115.     }
  116.  
  117.     public function getAllContents($request){
  118.       $request_string = $this->buildQueryString($request);
  119.       $url = 'https://conversation.8villages.com/1.0/contents/articles-questions-forums-storeItems-videos?'.$request_string;
  120.       return $this->apiGet($url);
  121.     }
  122.  
  123.     public function getRanking($request, $type = 'this_month'){
  124.       $request_string = $this->buildQueryString($request);
  125.       $url = 'https://analytic.8villages.com/questions?total=true&timeframe='.$type.'&'.$request_string.'&communityId='.getenv('COMMUNITY_ID');
  126.       return $this->apiGet($url);
  127.     }
  128.  
  129.     public function getResponses($request){
  130.       $request_string = $this->buildQueryString($request);
  131.       $url = 'https://conversation.8villages.com/1.0/contents/responses?'.$request_string;
  132.       return $this->apiGet($url);
  133.     }
  134.  
  135.         public function getWeather(){
  136.             $url = 'http://8villages.com/inbound/pull/weather?length=10';
  137.             return $this->apiGet($url);
  138.         }
  139.  
  140.         public function getPrice(){
  141.             $url = 'http://8villages.com/inbound/pull/price';
  142.             return $this->apiGet($url);
  143.         }
  144.  
  145.         public function getConversationDetail ($id) {
  146.             $url = 'https://conversation.8villages.com/1.0/content/'.$id;
  147.             return $this->apiGet($url);
  148.         }
  149.     public function getConversationResponse ($id) {
  150.       $url = 'https://conversation.8villages.com/1.0/contents/'.$id.'/responses';
  151.       return $this->apiGet($url);
  152.     }
  153.     public function getUserProfile ($id) {
  154.       $url = 'https://accounts.8villages.com/users/'.getenv('COMMUNITY_ID').'/'.$id;
  155.       return $this->apiGet($url);
  156.     }
  157.     public function getUserActivity ($id) {
  158.       $url = "https://conversation.8villages.com/1.0/contents/questions-responses?userId=".$id;
  159.       return $this->apiGet($url);
  160.     }
  161.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top