Abraham\TwitterOAuth\Util::buildHttpQuery PHP Method

buildHttpQuery() public static method

public static buildHttpQuery ( array $params ) : string
$params array
return string
    public static function buildHttpQuery(array $params)
    {
        if (empty($params)) {
            return '';
        }
        // Urlencode both keys and values
        $keys = Util::urlencodeRfc3986(array_keys($params));
        $values = Util::urlencodeRfc3986(array_values($params));
        $params = array_combine($keys, $values);
        // Parameters are sorted by name, using lexicographical byte value ordering.
        // Ref: Spec: 9.1.1 (1)
        uksort($params, 'strcmp');
        $pairs = [];
        foreach ($params as $parameter => $value) {
            if (is_array($value)) {
                // If two or more parameters share the same name, they are sorted by their value
                // Ref: Spec: 9.1.1 (1)
                // June 12th, 2010 - changed to sort because of issue 164 by hidetaka
                sort($value, SORT_STRING);
                foreach ($value as $duplicateValue) {
                    $pairs[] = $parameter . '=' . $duplicateValue;
                }
            } else {
                $pairs[] = $parameter . '=' . $value;
            }
        }
        // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
        // Each name-value pair is separated by an '&' character (ASCII code 38)
        return implode('&', $pairs);
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Make an HTTP request
  *
  * @param $url
  * @param $method
  * @param $headers
  * @param $postfields
  *
  * @return string
  * @throws TwitterOAuthException
  */
 private function request($url, $method, $headers, $postfields)
 {
     /* Curl settings */
     $options = array(CURLOPT_CAINFO => __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem', CURLOPT_CAPATH => __DIR__, CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array($headers, 'Expect:'), CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_URL => $url, CURLOPT_USERAGENT => $this->userAgent, CURLOPT_ENCODING => 'gzip');
     if (!empty($this->proxy)) {
         $options[CURLOPT_PROXY] = $this->proxy['CURLOPT_PROXY'];
         $options[CURLOPT_PROXYUSERPWD] = $this->proxy['CURLOPT_PROXYUSERPWD'];
         $options[CURLOPT_PROXYPORT] = $this->proxy['CURLOPT_PROXYPORT'];
         $options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;
         $options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
     }
     switch ($method) {
         case 'GET':
             if (!empty($postfields)) {
                 $options[CURLOPT_URL] .= '?' . Util::buildHttpQuery($postfields);
             }
             break;
         case 'POST':
             $options[CURLOPT_POST] = true;
             $options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields);
             break;
     }
     $curlHandle = curl_init();
     curl_setopt_array($curlHandle, $options);
     $response = curl_exec($curlHandle);
     $curlErrno = curl_errno($curlHandle);
     switch ($curlErrno) {
         case 28:
             throw new TwitterOAuthException('Request timed out.');
         case 51:
             throw new TwitterOAuthException('The remote servers SSL certificate or SSH md5 fingerprint failed validation.');
         case 56:
             throw new TwitterOAuthException('Response from server failed or was interrupted.');
     }
     $this->lastHttpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
     if (empty($this->proxy)) {
         list($header, $body) = explode("\r\n\r\n", $response, 2);
     } else {
         list(, $header, $body) = explode("\r\n\r\n", $response, 3);
     }
     list($this->lastHttpHeaders, $this->lastXHeaders) = $this->parseHeaders($header);
     $this->lastHttpInfo = curl_getinfo($curlHandle);
     curl_close($curlHandle);
     return $body;
 }
All Usage Examples Of Abraham\TwitterOAuth\Util::buildHttpQuery