/** * Returns the actual JSON body as an array. * * @author David Pauli <*****@*****.**> * @return mixed[] The body. * @since 0.2.1 */ public static function getJSONContent() { return JSONHandler::parseJSON(self::$CONTENT); }
/** * This send function sends a special command to the REST server with additional parameter. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.0.1 Use HTTPRequestMethod enum. * @since 0.1.0 Allow empty message body if the status code is 204. * @api * @param String command The path which is requested in the REST client. * @param String[] postfields Add specific parameters to the REST server. * @return mixed[] The returned elements as array. */ public static function send($command, $postfields = array()) { if (!InputValidator::isRESTCommand($command) || !self::$ISCONNECTED || !InputValidator::isArray($postfields)) { return null; } $protocol = self::$ISSSL ? "https" : "http"; $url = $protocol . "://" . self::$HOST . "/" . self::PATHTOREST . "/" . self::$SHOP . "/" . $command; $headers = array("Accept: " . self::HTTP_ACCEPT, "Content-Type: " . self::HTTP_CONTENT_TYPE); if (InputValidator::isAuthToken(self::$AUTHTOKEN)) { array_push($headers, "Authorization: Bearer " . self::$AUTHTOKEN); } $curl = curl_init($url); curl_setopt($curl, CURLOPT_FAILONERROR, 1); // show full errors curl_setopt($curl, CURLOPT_FORBID_REUSE, 0); // connection can be opened curl_setopt($curl, CURLOPT_FRESH_CONNECT, 0); // no new connection required curl_setopt($curl, CURLOPT_NOBODY, 0); // show body curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // get response as string curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); // no connection timeout curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 0); // no connection timeout curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); // cURL will choose the http version curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER); // understand ipv4 and ipv6 if (self::$ISSSL) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // don't check the peer ssl cerrificate curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS); curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_DEFAULT); // default ssl version } else { curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP); curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP); } switch (self::$HTTP_REQUEST_METHOD) { case HTTPRequestMethod::GET: curl_setopt($curl, CURLOPT_HTTPGET, 1); break; case HTTPRequestMethod::POST: $JSONpostfield = JSONHandler::createJSON($postfields); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTREDIR, 0); // don't post on redirects curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); break; case HTTPRequestMethod::PUT: $JSONpostfield = JSONHandler::createJSON($postfields); array_push($headers, "Content-Length: " . strlen($JSONpostfield)); curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); break; case HTTPRequestMethod::DELETE: $JSONpostfield = JSONHandler::createJSON($postfields); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); break; case HTTPRequestMethod::PATCH: $JSONpostfield = JSONHandler::createJSON($postfields); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); break; } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($curl); $info = curl_getinfo($curl); $error = curl_error($curl); curl_close($curl); $logMessage = self::$HTTP_REQUEST_METHOD . " " . $info["url"] . "<br/>" . "<strong>Response</strong>: " . $info["http_code"] . ": <pre>" . htmlspecialchars($response) . "</pre><br/>" . "<strong>Content-Type</strong>: " . $info["content_type"] . "<br/>" . "<strong>Size</strong> (Header/Request): " . $info["header_size"] . "/" . $info["request_size"] . " Bytes<br/>" . "<strong>Time</strong> (Total/Namelookup/Connect/Pretransfer/Starttransfer/Redirect): " . $info["total_time"] . " / " . $info["namelookup_time"] . " / " . $info["connect_time"] . " / " . $info["pretransfer_time"] . " / " . $info["starttransfer_time"] . " / " . $info["redirect_time"] . " seconds<br/>"; Logger::notify("<strong>HTTP-SEND</strong>:<br/>" . $logMessage); // if message body is empty this is allowed with 204 if (!$response && $info["http_code"] != "204") { Logger::error("Error with send REST client: " . $error); return null; } elseif (!in_array($info["http_code"], array("200", "201", "204"))) { Logger::warning("Get wrong response: " . $info["http_code"]); return null; } return JSONHandler::parseJSON($response); }