1 | <?php |
---|
2 | class N4D{ |
---|
3 | |
---|
4 | function __construct($server){ |
---|
5 | $this->server = $server; |
---|
6 | } |
---|
7 | |
---|
8 | public function execute( $method, $args, $timeout = 0 ){ |
---|
9 | $url = "https://".$this->server . ":9779"; |
---|
10 | $request = xmlrpc_encode_request($method, $args); |
---|
11 | |
---|
12 | $this->curl = curl_init(); |
---|
13 | $header[] = "Content-type: text/xml"; |
---|
14 | $header[] = "Content-length: ".strlen($request); |
---|
15 | |
---|
16 | curl_setopt($this->curl, CURLOPT_URL, $url); |
---|
17 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); |
---|
18 | curl_setopt($this->curl, CURLOPT_TIMEOUT, 10000); |
---|
19 | curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); |
---|
20 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request); |
---|
21 | |
---|
22 | if ($timeout!="0"){ |
---|
23 | curl_setopt($this->curl, CURLOPT_FRESH_CONNECT, true); // async |
---|
24 | curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout); // async |
---|
25 | } |
---|
26 | |
---|
27 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); |
---|
28 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
---|
29 | //curl_setopt($this->curl, CURLOPT_CAINFO, getcwd() . "/n4dcert.pem"); |
---|
30 | |
---|
31 | $resultrequest = curl_exec($this->curl); |
---|
32 | $request_error = curl_errno($this->curl); |
---|
33 | curl_close($this->curl); |
---|
34 | |
---|
35 | // Request fail. |
---|
36 | if ($request_error > 0 ){ |
---|
37 | throw new Exception('Curl exception ' . strval($request_error)); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | // This code is needed ???? |
---|
42 | // if ( gettype($)!="unknown type" && $request_error ) { |
---|
43 | // $xml_snippet = simplexml_load_string($data); |
---|
44 | // $json = json_encode($xml_snippet); |
---|
45 | // return $json; |
---|
46 | // } |
---|
47 | |
---|
48 | $xmlobj = xmlrpc_decode($resultrequest); |
---|
49 | $result = json_encode($xmlobj); |
---|
50 | // If result isn't json |
---|
51 | if ( $result == "" ) $result = $xmlobj ; |
---|
52 | return $result; |
---|
53 | } |
---|
54 | } |
---|
55 | ?> |
---|