Advertisement
Guest User

checkstatus

a guest
Feb 5th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1. <?php
  2. // Global Var
  3.    
  4.     $base_url='http://apollo.gits.id:9000/v1';
  5.    
  6.    
  7. //Get Token
  8.  
  9.     $client_id='ahs';
  10.     $client_secret='ahs_active';
  11.     $grant_type='password';
  12.     $username='Danone';
  13.     $password='Danone';
  14.  
  15.     $api_path='/oauth/login';
  16.    
  17.     $constracttoken=$base_url.$api_path;
  18.    
  19.     $callAPItoken=CallAPI('POST',$constracttoken,array('client_id'=>$client_id,'client_secret'=>$client_secret,'grant_type'=>$grant_type,'username'=>$username,'password'=>$password));
  20.     $jsdecodetoken=json_decode($callAPItoken);
  21.    
  22.     $accesstoken=$jsdecodetoken->access_token;
  23.    
  24. //  echo $accesstoken."<br>";
  25.     //$constract=$base_url.$api_path.'?client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type='.$grant_type.'&username='.$username.'&password='.$password;
  26.     //echo $constract
  27.     //$hitURL=file_get_contents($constract);
  28.     //$response=json_decode($hitURL);
  29.     //echo $hitURL;
  30.     //$test=file_get_contents('http://google.com');
  31.     //echo $test;  
  32.     //http://apollo.gits.id:9000/v1/oauth/login?client_id=ahs&client_secret=ahs_active&grant_type=password&username=Danone&password=Danone
  33.     //$hitURL=file_get_contents('http://localhost:90/json/andy.json');
  34.  
  35.  
  36. //Use Token to Get Status
  37.  
  38.     $status_path='/consumers';
  39.     $constractstatus=$base_url.$status_path;
  40.    
  41.     $search_type='phone_no';
  42. //  $phone_number='6285224265251';
  43. //  $phone_number='6282138262565';
  44.    
  45.     $getresponsestatus=file_get_contents($constractstatus.'?accessToken='.$accesstoken.'&search_type='.$search_type.'&search='.$phone_number);
  46. //  echo $getresponsestatus;
  47.  
  48.     $jsdecodestatus=json_decode($getresponsestatus);
  49. //  $consumerstatuscomplete=$jsdecodestatus->data->consumers[0]->status;
  50.     $consumerstatus=$jsdecodestatus->data->consumers;
  51.    
  52. //  echo $getresponsestatus."<br>";
  53. //  echo $consumerstatus."<br>";
  54.  
  55. /*  if ($consumerstatuscomplete="active") {
  56.         echo "true";
  57.     } elseif($consumerstatuscomplete=""){
  58.         echo "false";
  59.     }  
  60. */
  61.  
  62.     if (!empty($consumerstatus)) {
  63.         //echo 'terdaftar';
  64.         $status='terdaftar';
  65.     } else {
  66.         //echo 'tidak terdaftar';
  67.         $status='tidak terdaftar';
  68.     }
  69.  
  70.     $showtext_1='{';
  71.     $showtext_2='"messages": [';
  72.     $showtext_3='{"text": "Status kamu adalah"},';
  73.     $showtext_4='{"text": "'.$status.'"}';
  74.     $showtext_5=']';
  75.     $showtext_6='}';
  76.  
  77.     $showtext=$showtext_1.$showtext_2.$showtext_3.$showtext_4.$showtext_5.$showtext_6;
  78.  
  79.     $jsonencode=json_encode($showtext);
  80.  
  81.     $patterns = array();
  82.     $patterns[0] = '/\\\\\"/';
  83.     $replacements = array();
  84.     $replacements[2] = "\"";
  85.  
  86.     $jsonencode= preg_replace($patterns, $replacements, $jsonencode);
  87.     $jsonencode=trim($jsonencode, '"');
  88.  
  89.     echo $jsonencode;  
  90.    
  91. /*  // Evaluates as true because $var is set
  92.     if (isset($consumerstatus)) {
  93.         if ($consumerstatuscomplete="active") {
  94.             echo 'terdaftar';
  95.             }
  96.     }
  97. */
  98.    
  99. //Reformat to Chatbot Client
  100.  
  101. //New Function
  102.  
  103.     // Method: POST, PUT, GET etc
  104.     // Data: array("param" => "value") ==> index.php?param=value
  105.     function CallAPI($method, $url, $data = false)
  106.     {
  107.         $curl = curl_init();
  108.  
  109.         switch ($method)
  110.         {
  111.             case "POST":
  112.                 curl_setopt($curl, CURLOPT_POST, 1);
  113.  
  114.                 if ($data)
  115.                     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  116.                 break;
  117.                
  118.             case "PUT":
  119.                 curl_setopt($curl, CURLOPT_PUT, 1);
  120.                 break;
  121.             default:
  122.                 if ($data)
  123.                     $url = sprintf("%s?%s", $url, http_build_query($data));
  124.                    
  125.             case "GET":
  126.                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  127.                 if ($data)
  128.                     curl_setopt($curl, CURLOPT_RETURNTRANSFER, $data);
  129.                 break;
  130.         }
  131.  
  132.         // Optional Authentication:
  133.         curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  134.         curl_setopt($curl, CURLOPT_USERPWD, "username:password");
  135.  
  136.         curl_setopt($curl, CURLOPT_URL, $url);
  137.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  138.  
  139.         $result = curl_exec($curl);
  140.  
  141.         curl_close($curl);
  142.  
  143.         return $result;
  144.     }
  145.  
  146. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement