ForkAPI::doCall PHP Method

doCall() private method

Make the call
private doCall ( string $method, array[optional] $parameters = [], bool[optional] $authenticate = true, bool[optional] $usePOST = false ) : string
$method string The method to call.
$parameters array[optional]
$authenticate bool[optional]
$usePOST bool[optional]
return string
    private function doCall($method, $parameters = array(), $authenticate = true, $usePOST = false)
    {
        // redefine
        $method = (string) $method;
        $parameters = (array) $parameters;
        $authenticate = (bool) $authenticate;
        // add required parameters
        $queryStringParameters['method'] = (string) $method;
        // authentication stuff
        if ($authenticate) {
            // get keys
            $publicKey = $this->getPublicKey();
            $privateKey = $this->getPrivateKey();
            // validate
            if ($publicKey == '' || $privateKey == '') {
                throw new ForkAPIException('This method (' . $method . ') requires authentication, provide a public and private key.');
            }
            // add prams
            $queryStringParameters['public_key'] = $publicKey;
            $queryStringParameters['nonce'] = time();
            $queryStringParameters['secret'] = md5($publicKey . $privateKey . $queryStringParameters['nonce']);
        }
        // build URL
        $url = self::API_URL . '/' . self::API_VERSION . '/rest.php?' . http_build_query($queryStringParameters, null, '&', PHP_QUERY_RFC3986);
        // use POST?
        if ($usePOST) {
            // set POST
            $options[CURLOPT_POST] = true;
            // add data if needed
            if (!empty($parameters)) {
                $options[CURLOPT_POSTFIELDS] = array('data' => json_encode($parameters));
            }
        } else {
            // any data if needed
            if (!empty($parameters)) {
                // build querystring
                $queryString = http_build_query(array('data' => json_encode($parameters)), null, '&', PHP_QUERY_RFC3986);
                // prepend
                $url .= '&' . $queryString;
            }
        }
        // set options
        $options[CURLOPT_URL] = $url;
        $options[CURLOPT_PORT] = self::API_PORT;
        $options[CURLOPT_USERAGENT] = $this->getUserAgent();
        if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
            $options[CURLOPT_FOLLOWLOCATION] = true;
        }
        $options[CURLOPT_RETURNTRANSFER] = true;
        $options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
        // init
        $curl = curl_init();
        // set options
        curl_setopt_array($curl, $options);
        // execute
        $response = curl_exec($curl);
        $headers = curl_getinfo($curl);
        // fetch errors
        $errorNumber = curl_errno($curl);
        $errorMessage = curl_error($curl);
        // close
        curl_close($curl);
        // we expect XML so decode it
        $xml = @simplexml_load_string($response, null, LIBXML_NOCDATA);
        // validate XML
        if ($xml === false) {
            throw new ForkAPIException('Invalid XML-response.');
        }
        // is error?
        if (!isset($xml['status']) || (string) $xml['status'] != 'ok') {
            // is it a response error
            if (isset($xml->error)) {
                throw new ForkAPIException((string) $xml->error);
            } else {
                throw new ForkAPIException('Invalid XML-response.');
            }
        }
        // return
        return $xml;
    }