Example #1
0
<?xml version="1.0" encoding="UTF-8"?>
<json2xml>
\t<names>a</names>
\t<names>b</names>
\t<names>c</names>
\t<id>a</id>
\t<id>b</id>
\t<id>c</id>
</json2xml>
END;
$xml2 = <<<END
<?xml version="1.0" encoding="UTF-8"?>
<json2xml>
  <Liste>
    <Liste>Roland</Liste>
    <Liste>Peter</Liste>
    <Liste>Mario</Liste>
  </Liste>
  <Liste2>
    <Liste2>A</Liste2>
    <Liste2>B</Liste2>
    <Liste2>C</Liste2>
  </Liste2>
</json2xml>
END;
print XML::fromJSON(XML::toJSON($xml)) . "\n\n";
print XML::fromJSON(XML::toJSON($xml2)) . "\n\n";
print XML::fromJSON('Hallo') . "\n";
print XML::fromJSON(['Hallo' => '', 'Welt' => '']) . "\n";
print XML::fromJSON(['Liste' => ['Roland', 'Peter', 'Mario'], 'Liste2' => ['A', 'B', 'C']]) . "\n";
 /**
  * Execute API call.
  * 
  * If set('auth', 'basic_auth') use set('token, 'login:password').
  * Use set('accept', 'application/xml') and set('content', 'application/xml') to send and receive xml.
  *
  * @param map|string $data use xml if content=application/xml (default = null)
  * @return bool (status=200 true, otherwise false)
  */
 public function exec($data = null)
 {
     $required = ['method', 'uri', 'url'];
     foreach ($required as $key) {
         if (empty($this->{$key})) {
             throw new Exception('missing parameter', $key);
         }
     }
     $ch = curl_init();
     $header = array();
     if (!empty($this->token)) {
         if (empty($this->auth)) {
             throw new Exception('missing parameter', 'auth');
         } else {
             if ($this->auth == 'request' && is_array($data)) {
                 $data['api_token'] = $this->token;
             } else {
                 if ($this->auth == 'header') {
                     array_push($header, 'X-AUTH-TOKEN: ' . $this->token);
                 } else {
                     if ($this->auth == 'basic_auth') {
                         curl_setopt($ch, CURLOPT_USERPWD, $conf['api_token']);
                     }
                 }
             }
         }
     }
     if (!empty($this->accept)) {
         array_push($header, 'ACCEPT: ' . $this->accept);
     }
     if (!empty($this->content)) {
         array_push($header, 'CONTENT-TYPE: ' . $this->content);
         if (is_string($data)) {
             // raw data request
             array_push($header, 'X-HTTP-Method-Override: ' . $this->method);
             if ($this->content == 'application/xml' && is_array($data)) {
                 $data = XML::fromJSON($data);
             }
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         }
     }
     if (count($header) > 0) {
         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     }
     $uri_append = '';
     if ($this->method == 'GET') {
         if (is_array($data) && count($data) > 0) {
             $uri_append = '?' . http_build_query($data);
         }
     } else {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
         if (($this->method == 'PUT' || $this->method == 'DELETE') && is_array($data) && count($data) > 0) {
             $data = http_build_query($data);
         }
         if (!is_null($data)) {
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         }
     }
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_URL, $this->url . '/' . $this->uri . $uri_append);
     $this->result = curl_exec($ch);
     $this->info = curl_getinfo($ch);
     $this->status = intval($this->info['http_code']);
     curl_close($ch);
     if ($this->accept == 'application/json') {
         $this->result = json_decode($this->result, true);
     }
     return $this->status === 200;
 }