TijsVerkoyen\Twitter\Twitter::calculateBaseString PHP Method

calculateBaseString() protected method

All OAuth 1.0 requests use the same basic algorithm for creating a signature base string and a signature. The signature base string is composed of the HTTP method being used, followed by an ampersand ("&") and then the URL-encoded base URL being accessed, complete with path (but not query parameters), followed by an ampersand ("&"). Then, you take all query parameters and POST body parameters (when the POST body is of the URL-encoded type, otherwise the POST body is ignored), including the OAuth parameters necessary for negotiation with the request at hand, and sort them in lexicographical order by first parameter name and then parameter value (for duplicate parameters), all the while ensuring that both the key and the value for each parameter are URL encoded in isolation. Instead of using the equals ("=") sign to mark the key/value relationship, you use the URL-encoded form of "%3D". Each parameter is then joined by the URL-escaped ampersand sign, "%26".
protected calculateBaseString ( string $url, string $method, array $parameters ) : string
$url string The URL.
$method string The method to use.
$parameters array The parameters.
return string
    protected function calculateBaseString($url, $method, array $parameters)
    {
        // redefine
        $url = (string) $url;
        $parameters = (array) $parameters;
        // init var
        $pairs = array();
        $chunks = array();
        // sort parameters by key
        uksort($parameters, 'strcmp');
        // loop parameters
        foreach ($parameters as $key => $value) {
            // sort by value
            if (is_array($value)) {
                $parameters[$key] = natsort($value);
            }
        }
        // process queries
        foreach ($parameters as $key => $value) {
            // only add if not already in the url
            if (substr_count($url, $key . '=' . $value) == 0) {
                $chunks[] = self::urlencode_rfc3986($key) . '%3D' . self::urlencode_rfc3986($value);
            }
        }
        // buils base
        $base = $method . '&';
        $base .= urlencode($url);
        $base .= substr_count($url, '?') ? '%26' : '&';
        $base .= implode('%26', $chunks);
        $base = str_replace('%3F', '&', $base);
        // return
        return $base;
    }
Twitter