Sailthru_Client::httpRequestWithoutCurl PHP Method

httpRequestWithoutCurl() protected method

Adapted from: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
protected httpRequestWithoutCurl ( string $action, array $data, string $method = 'POST', array $options = [] ) : string
$action string
$data array
$method string
$options array
return string
    protected function httpRequestWithoutCurl($action, $data, $method = 'POST', $options = [])
    {
        if ($this->fileUpload === true) {
            $this->fileUpload = false;
            throw new Sailthru_Client_Exception('cURL extension is required for the request with file upload', Sailthru_Client_Exception::CODE_GENERAL);
        }
        $url = $this->api_uri . "/" . $action;
        $params = ['http' => ['method' => $method, 'ignore_errors' => true]];
        if ($method == 'POST') {
            $params['http']['content'] = is_array($data) ? http_build_query($data, '', '&') : $data;
        } else {
            $url .= '?' . http_build_query($data, '', '&');
        }
        $params['http']['header'] = "User-Agent: {$this->user_agent_string}\nContent-Type: application/x-www-form-urlencoded";
        $ctx = stream_context_create($params);
        $fp = @fopen($url, 'rb', false, $ctx);
        if (!$fp) {
            throw new Sailthru_Client_Exception("Unable to open stream: {$url}", Sailthru_Client_Exception::CODE_GENERAL);
        }
        $response = @stream_get_contents($fp);
        if ($response === false) {
            throw new Sailthru_Client_Exception("No response received from stream: {$url}", Sailthru_Client_Exception::CODE_RESPONSE_EMPTY);
        }
        return $response;
    }