source: internals/2017/FacebookAppAndFacebookPost/src/Adolfo/Commands/Search.php @ 16741

Last change on this file since 16741 was 16741, checked in by amoreno, 2 years ago

First commit

File size: 2.8 KB
Line 
1<?php
2
3namespace Adolfo\Commands;
4
5class search
6{
7
8  public $isValid = true;
9  public $receivedMSG;
10  public $storeIndex = 0;
11  public $appIndex;
12  public $app;
13  public $store;
14  public $answer = ["attachment"=>[
15    "type"=>"template",
16    "payload"=>[
17      "template_type"=>"generic",
18      "elements"=>[
19
20      ]
21    ]
22  ]];
23
24  public function __construct($receivedMSG = array())
25  {
26    $this->receivedMS = $receivedMSG;
27    $this->appIndex = array_search('app',$this->receivedMS);
28    for($i=1;$i<$this->appIndex;$i++){
29      $this->store[]=$this->receivedMS[$i];
30    }
31    for($i=$this->appIndex+1;$i<count($this->receivedMS);$i++){
32      $this->app[]=$this->receivedMS[$i];
33    }
34    $aplication = array_shift($this->app);
35    $this->store = $this->app[1];
36    $this->app = $aplication;
37    echo "Store: ";
38    var_dump($this->store);
39    echo "App: ";
40    var_dump($this->app);
41  }
42
43  public function getData()
44  {
45    $encodedApp = urlencode($this->app);
46    $encodedStore = urlencode($this->store);
47    $contents = file_get_contents("https://ws75.aptoide.com/api/7/apps/search/query={$encodedApp}/store_name={$encodedStore}/limit=5");
48    if($contents!==false){
49      return $contents;
50    }else{
51        $this->isValid = false;
52        return null;
53    }
54  }
55
56  public function parseData($getDataReturn)
57  {
58    $toAdd = [
59      "title"=>"",
60      "item_url"=>"",
61      "image_url"=>"",
62      "subtitle"=>"",
63      "buttons"=>[
64        [
65          "type"=>"web_url",
66          "url"=>"",
67          "title"=>"Download app!"
68        ],
69        [
70          "type"=>"web_url",
71          "url"=>"en.aptoide.com",
72          "title"=>"Go to Aptoide"
73        ],
74        [
75          "type"=>"element_share"
76        ]
77      ]
78    ];
79    if($this->isValid){
80      $parseThis = json_decode($getDataReturn);
81      for($i = 0; $i < count($parseThis->datalist->list); $i++){
82        $app = file_get_contents("https://ws75.aptoide.com/api/7/app/getMeta?app_id=".$parseThis->datalist->list[$i]->id);
83        $decodedApp = json_decode($app);
84        if(strcmp($decodedApp->info->status,"OK")==0){
85          $toAdd['title'] = $decodedApp->data->name;
86          $toAdd['buttons'][0]['url'] = "https://en.aptoide.com/thank-you?app_id={$decodedApp->data->id}&store_name={$decodedApp->data->store->name}";
87          $toAdd['image_url'] = $decodedApp->data->icon;
88          $toAdd['item_url'] = $decodedApp->data->uname.".en.aptoide.com";
89          $toAdd['subtitle'] = $decodedApp->data->media->description;
90          array_push($this->answer['attachment']['payload']['elements'], $toAdd);
91        }
92      }
93    }else{
94      $this->answer = "The search is not valid or doesn't exist.";
95    }
96  }
97
98  public function createMSG()
99  {
100    if($this->isValid){
101      return $this->answer['attachment']['payload'];
102    }else{
103      return $this->answer;
104    }
105  }
106
107  public function isOK()
108  {
109    return $this->isValid;
110  }
111
112}
113
114?>
Note: See TracBrowser for help on using the repository browser.