<?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";
/** * Return plugin result = $plugin->tok_NAME($param, $arg). * If callback mode is not TokPlugin::TOKCALL preprocess param and arg. Example: * * Convert param into vector and arg into map if plugin $name is configured with * TokPlugin::KV_BODY | TokPlugin::PARAM_CSLIST | TokPlugin::REQUIRE_BODY | TokPlugin::REQUIRE_PARAM * * @throws rkphplib\Exception 'plugin parameter missing', 'invalid plugin parameter', 'plugin body missing', 'invalid plugin body' * @param string $name * @param string $param * @param string $arg (default = null = no argument) * @return string */ private function _call_plugin($name, $param, $arg = null) { if ($this->_plugin[$name][1] & TokPlugin::TOKCALL) { return call_user_func(array($this->_plugin[$name][0], 'tokCall'), $name, $param, $arg); } $func = 'tok_' . $name; $pconf = $this->_plugin[$name][1]; $plen = strlen($param); $alen = strlen($arg); if (!empty($this->_plugin["{$name}:{$param}"])) { $func = 'tok_' . $name . '_' . $param; $pconf = $this->_plugin["{$name}:{$param}"][1]; $plen = 0; } if ($pconf & TokPlugin::REQUIRE_PARAM && $plen == 0) { throw new Exception('plugin parameter missing', "plugin={$name}"); } else { if ($pconf & TokPlugin::NO_PARAM && $plen > 0) { throw new Exception('invalid plugin parameter', "plugin={$name} param={$param}"); } } if ($pconf & TokPlugin::REQUIRE_BODY && $alen == 0) { throw new Exception('plugin body missing', "plugin={$name}"); } else { if ($pconf & TokPlugin::NO_BODY && $alen > 0) { throw new Exception('invalid plugin body', "plugin={$name} arg={$arg}"); } } if ($pconf & TokPlugin::PARAM_LIST || $pconf & TokPlugin::PARAM_CSLIST) { require_once __DIR__ . '/lib/split_str.php'; $delim = $pconf & TokPlugin::PARAM_LIST ? ':' : ','; $param = lib\split_str($delim, $param); } if ($pconf & TokPlugin::KV_BODY) { require_once __DIR__ . '/lib/conf2kv.php'; $arg = lib\conf2kv($arg); } else { if ($pconf & TokPlugin::JSON_BODY) { require_once __DIR__ . '/JSON.class.php'; $arg = JSON::decode($arg); } else { if ($pconf & TokPlugin::CSLIST_BODY || $pconf & TokPlugin::LIST_BODY) { require_once __DIR__ . '/lib/split_str.php'; $delim = $pconf & TokPlugin::CSLIST_BODY ? ',' : '|#|'; $arg = lib\split_str($delim, $arg); } else { if ($pconf & TokPlugin::XML_BODY) { require_once __DIR__ . '/XML.class.php'; $arg = XML::toJSON($arg); } } } } $res = ''; if ($pconf & TokPlugin::NO_PARAM) { $res = call_user_func(array($this->_plugin[$name][0], $func), $arg); } else { if ($pconf & TokPlugin::NO_BODY) { $res = call_user_func(array($this->_plugin[$name][0], $func), $param); } else { $res = call_user_func(array($this->_plugin[$name][0], $func), $param, $arg); } } return $res; }
/** * 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; }