Unirest\Request::send PHP Method

send() public static method

Send a cURL request
public static send ( Unirest\Method | string $method, string $url, mixed $body = null, array $headers = [], string $username = null, string $password = null ) : unirest\Response
$method Unirest\Method | string HTTP method to use
$url string URL to send the request to
$body mixed request body
$headers array additional headers to send
$username string Authentication username (deprecated)
$password string Authentication password (deprecated)
return unirest\Response
    public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
    {
        self::$handle = curl_init();
        if ($method !== Method::GET) {
            if ($method === Method::POST) {
                curl_setopt(self::$handle, CURLOPT_POST, true);
            } else {
                curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
            }
            curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
        } elseif (is_array($body)) {
            if (strpos($url, '?') !== false) {
                $url .= '&';
            } else {
                $url .= '?';
            }
            $url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
        }
        $curl_base_options = [CURLOPT_URL => self::encodeUrl($url), CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 10, CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers), CURLOPT_HEADER => true, CURLOPT_SSL_VERIFYPEER => self::$verifyPeer, CURLOPT_SSL_VERIFYHOST => self::$verifyHost === false ? 0 : 2, CURLOPT_ENCODING => ''];
        curl_setopt_array(self::$handle, self::mergeCurlOptions($curl_base_options, self::$curlOpts));
        if (self::$socketTimeout !== null) {
            curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
        }
        if (self::$cookie) {
            curl_setopt(self::$handle, CURLOPT_COOKIE, self::$cookie);
        }
        if (self::$cookieFile) {
            curl_setopt(self::$handle, CURLOPT_COOKIEFILE, self::$cookieFile);
            curl_setopt(self::$handle, CURLOPT_COOKIEJAR, self::$cookieFile);
        }
        // supporting deprecated http auth method
        if (!empty($username)) {
            curl_setopt_array(self::$handle, array(CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $username . ':' . $password));
        }
        if (!empty(self::$auth['user'])) {
            curl_setopt_array(self::$handle, array(CURLOPT_HTTPAUTH => self::$auth['method'], CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass']));
        }
        if (self::$proxy['address'] !== false) {
            curl_setopt_array(self::$handle, array(CURLOPT_PROXYTYPE => self::$proxy['type'], CURLOPT_PROXY => self::$proxy['address'], CURLOPT_PROXYPORT => self::$proxy['port'], CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'], CURLOPT_PROXYAUTH => self::$proxy['auth']['method'], CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']));
        }
        $response = curl_exec(self::$handle);
        $error = curl_error(self::$handle);
        $info = self::getInfo();
        if ($error) {
            throw new Exception($error);
        }
        // Split the full response in its headers and body
        $header_size = $info['header_size'];
        $header = substr($response, 0, $header_size);
        $body = substr($response, $header_size);
        $httpCode = $info['http_code'];
        return new Response($httpCode, $body, $header, self::$jsonOpts);
    }

Usage Example

Example #1
0
 public function make_request($options)
 {
     $injector = Injector::getInstance();
     $url = $options['url'];
     $method = $options['method'];
     $headers = $options['headers'];
     $body = isset($options['body']) ? $options['body'] : '';
     $response = null;
     if (isset($options['qs'])) {
         $qs = http_build_query($options['qs']);
         $url .= '?' . $qs;
     }
     $url = str_replace('%2C', ',', $url);
     if (isset($headers['Content-Type']) && $headers['Content-Type'] == 'application/json' && is_array($body)) {
         $body = json_encode($body);
     }
     Request::verifyPeer($injector->ssl_verification);
     $response = Request::send($options['method'], $url, $body, $headers);
     return $response;
 }