1+ <?php
2+ namespace Splitice \BuyVM ;
3+
4+ /**
5+ * Stallion 'SolusVM like' API client
6+ * @package Splitice\BuyVM
7+ */
8+ class BuyVMApiClient implements IBuyVMApiClient
9+ {
10+ const URL = "https://manage.buyvm.net/api/client/command.php " ;
11+ private $ ch ;
12+ private $ key ;
13+ private $ hash ;
14+
15+ function __construct ($ key , $ hash , $ url = self ::URL ){
16+ $ this ->key = $ key ;
17+ $ this ->hash = $ hash ;
18+
19+ //Setup curl
20+ $ this ->ch = curl_init ($ url );
21+ curl_setopt ($ this ->ch , CURLOPT_POST , true );
22+ curl_setopt ($ this ->ch , CURLOPT_TIMEOUT , 30 );
23+ curl_setopt ($ this ->ch , CURLOPT_FRESH_CONNECT , 1 );
24+ curl_setopt ($ this ->ch , CURLOPT_SSL_VERIFYPEER , false );
25+ curl_setopt ($ this ->ch , CURLOPT_SSL_VERIFYHOST , false );
26+ curl_setopt ($ this ->ch , CURLOPT_HEADER , 0 );
27+ curl_setopt ($ this ->ch , CURLOPT_RETURNTRANSFER , 1 );
28+ curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , array ('Expect: ' ));
29+ }
30+
31+ function __destruct (){
32+ curl_close ($ this ->ch );
33+ }
34+
35+ private function execute ($ post ){
36+ //Set authentication
37+ $ post ['key ' ] = $ this ->key ;
38+ $ post ['hash ' ] = $ this ->hash ;
39+
40+ //set post data
41+ curl_setopt ($ this ->ch , CURLOPT_POSTFIELDS , $ post );
42+
43+ //Execute request
44+ $ data = curl_exec ($ this ->ch );
45+ $ code = curl_getinfo ($ this ->ch , CURLINFO_HTTP_CODE );
46+
47+ //Handle response
48+ if ($ code == 200 ){
49+ preg_match_all ('/<(.*?)>([^<]+)<\/ \\1>/i ' , $ data , $ match );
50+ $ result = array ();
51+
52+ foreach ($ match [1 ] as $ x => $ y ) {
53+ $ result [$ y ] = $ match [2 ][$ x ];
54+ }
55+
56+ return $ result ;
57+ }else {
58+ throw new ApiTransportException ($ code );
59+ }
60+ }
61+
62+ function execute_action ($ action , $ data = array ()){
63+ $ data ['action ' ] = $ action ;
64+ return $ this ->execute ($ data );
65+ }
66+
67+ function execute_info ($ fields ){
68+ $ post = array ();
69+ foreach ($ fields as $ f ){
70+ $ post [$ f ] = 'true ' ;
71+ }
72+ return $ this ->execute ($ post );
73+ }
74+ }
0 commit comments