Cloudinary\Uploader::call_api PHP Method

call_api() public static method

public static call_api ( $action, $params, $options = [], $file = NULL )
    public static function call_api($action, $params, $options = array(), $file = NULL)
    {
        $return_error = \Cloudinary::option_get($options, "return_error");
        if (!\Cloudinary::option_get($options, "unsigned")) {
            $params = \Cloudinary::sign_request($params, $options);
        }
        $api_url = \Cloudinary::cloudinary_api_url($action, $options);
        $ch = curl_init($api_url);
        $post_params = array();
        foreach ($params as $key => $value) {
            if (is_array($value)) {
                $i = 0;
                foreach ($value as $item) {
                    $post_params[$key . "[{$i}]"] = $item;
                    $i++;
                }
            } else {
                $post_params[$key] = $value;
            }
        }
        if ($file) {
            if (!preg_match('/^@|^ftp:|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\\/+\\n=]+)$/', $file)) {
                if (function_exists("curl_file_create")) {
                    $post_params['file'] = curl_file_create($file);
                    $post_params['file']->setPostFilename($file);
                } else {
                    $post_params["file"] = "@" . $file;
                }
            } else {
                $post_params["file"] = $file;
            }
        }
        curl_setopt($ch, CURLOPT_POST, true);
        $timeout = \Cloudinary::option_get($options, "timeout", \Cloudinary::config_get("timeout", 60));
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        $connection_timeout = \Cloudinary::option_get($options, "connection_timeout", \Cloudinary::config_get("connection_timeout"));
        if ($connection_timeout != NULL) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connection_timeout);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
        curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "cacert.pem");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        # no effect since PHP 5.1.3
        curl_setopt($ch, CURLOPT_USERAGENT, \Cloudinary::userAgent());
        curl_setopt($ch, CURLOPT_PROXY, \Cloudinary::option_get($options, "api_proxy", \Cloudinary::config_get("api_proxy")));
        $range = \Cloudinary::option_get($options, "content_range");
        if ($range != NULL) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Range: ' . $range));
        }
        $response = curl_exec($ch);
        $curl_error = NULL;
        if (curl_errno($ch)) {
            $curl_error = curl_error($ch);
        }
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $response_data = $response;
        curl_close($ch);
        if ($curl_error != NULL) {
            throw new \Cloudinary\Error("Error in sending request to server - " . $curl_error);
        }
        if ($code != 200 && $code != 400 && $code != 500 && $code != 401 && $code != 404) {
            throw new \Cloudinary\Error("Server returned unexpected status code - " . $code . " - " . $response_data, $code);
        }
        $result = json_decode($response_data, TRUE);
        if ($result == NULL) {
            throw new \Cloudinary\Error("Error parsing server response (" . $code . ") - " . $response_data);
        }
        if (isset($result["error"])) {
            if ($return_error) {
                $result["error"]["http_code"] = $code;
            } else {
                throw new \Cloudinary\Error($result["error"]["message"], $code);
            }
        }
        return $result;
    }