.
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | WebMaster v0.1
2 | A small REST query library for PocketMine-MP
3 |
4 | Types of Query:
5 |
6 |
7 |
8 |
9 | - [x] GET
10 | - [x] POST
11 | - [ ] PUT
12 | - [ ] PATCH
13 | - [ ] DELETE
14 | - [ ] OPTIONS
15 | - [ ] HEAD
16 |
17 |
18 | METHOD | EXAMPLE
19 | ------------- | -------------
20 | GET | []
21 | POST | ['field1' => 'data', 'field2' => 'data2']
22 |
23 |
24 |
25 | 🔨 Usage:
26 |
27 |
28 |
29 | The WebMaster class receives 3 parameters as a constructor function, first the address, according to the query context (which receives the query result as a parameter), and finally the array with fields.
30 |
31 |
32 |
33 |
34 | - How to correctly call the class:
35 |
36 |
37 | ```php
38 | $query = new \your\directory\WebMaster('https://localhost', function($result) { var_dump($request); }, [/*WHAT COMES HERE IS REGARDING THE TABLE ABOVE...*/]);
39 | ```
40 |
--------------------------------------------------------------------------------
/WebMaster.php:
--------------------------------------------------------------------------------
1 | < | | | |
8 | * | |____ | |____ / ____ \ | | \ \ / . \ | |__| |
9 | * |______| |______| /_/ \_\ |_| \_\ /_/ \_\ |_____/
10 | *
11 | * This API was developed by LearXD for other uses, it is allowed to modify
12 | * almost everything here! Leave my watermark to help me ;)
13 | *
14 | * Twitter: @XDLear
15 | *
16 | */
17 |
18 | namespace your/directory {
19 |
20 | use pocketmine\Server;
21 |
22 | class WebMaster
23 | {
24 |
25 | /**
26 | * @var array
27 | */
28 | protected static $process = [];
29 |
30 | /**
31 | * @param string $url
32 | * @param array $data
33 | * @param callable $function
34 | * @return bool
35 | */
36 | public static function createProcess(string $url, callable $function, array $data = []): bool
37 | {
38 | self::$process[$pid = count(self::$process)] = $function;
39 | Server::getInstance()->getScheduler()->scheduleAsyncTask(new RequestAsync($pid, $url, $data));
40 | return true;
41 | }
42 |
43 | /**
44 | * @param int $pid
45 | * @param array $result
46 | * @return bool
47 | */
48 | public static function processResult(int $pid, array $result): bool
49 | {
50 | $callable = self::$process[$pid];
51 | $callable->call(Server::getInstance(), $result);
52 |
53 | unset(self::$process[$pid]);
54 | return true;
55 | }
56 |
57 | }
58 |
59 | class RequestAsync extends \pocketmine\scheduler\AsyncTask
60 | {
61 |
62 | /**
63 | * @var int
64 | */
65 | private $pid = 0;
66 |
67 | /**
68 | * @var string
69 | */
70 | protected $url = "";
71 | /**
72 | * @var array
73 | */
74 | protected $data;
75 |
76 |
77 | /**
78 | * RequestAsync constructor.
79 | * @param int $pid
80 | * @param string $url
81 | * @param array $pdata
82 | */
83 | public function __construct(int $pid, string $url, array $pdata = [])
84 | {
85 | $this->pid = $pid;
86 | $this->url = serialize($url);
87 | $this->data = serialize($pdata);
88 | }
89 |
90 | public function onRun()
91 | {
92 | $data = unserialize($this->data);
93 | $url = unserialize($this->url);
94 |
95 | $curl = curl_init($url);
96 | curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 OPR/77.0.4054.298"], []));
97 |
98 | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
99 | curl_setopt($curl, CURLOPT_TIMEOUT, 10);
100 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
101 |
102 | if ($curl) {
103 | $result = [];
104 | if (\count($data) <= 0) {
105 | curl_setopt($curl, CURLOPT_AUTOREFERER, true);
106 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
107 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
108 | curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
109 | curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
110 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
111 | } else {
112 | curl_setopt($curl, CURLOPT_POST, true);
113 | curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
114 | }
115 | $result = curl_exec($curl);
116 | curl_close($curl);
117 | $this->setResult(json_decode($result, true) ?? [ 'error' => 'Unable to perform the search!']);
118 | } else {
119 | $this->setResult([ 'error' => 'Unable to resolve search data!' ], true);
120 | }
121 | }
122 |
123 | /**
124 | * @param Server $server
125 | */
126 | public function onCompletion(Server $server)
127 | {
128 | WebMaster::processResult($this->pid, $this->getResult());
129 | }
130 | }
131 | }
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------