title: Generating OAuth 1 Signature in PHP
body: <p>I'm trying to connect to the <a href="https://developers.liveperson.com/engagement-history-api-methods.html" rel="noreferrer">LivePerson Engagement History API</a> and I'm running into an issue that I believe is related to the signature being generated. </p>
  
  <p>First off, the API already provides the necessary consumer key, consumer secret, access token, and token secret. So I don't have to go through the process of retrieving those. In order to access their API I just have to <a href="https://developers.liveperson.com/guides-gettingstarted.html" rel="noreferrer">provide the auth header</a>. I've mocked everything up using Postman and it all works correctly. The issue is when I try to generate my own timestamp/nonce/signature in my class. </p>
  
  <p>Here's the method from my class that sends the cURL request:</p>
  
  <pre><code>private function execute($options = array())
  {
      if (!isset($options['url'])) {
          return;
      }
  
      $ch = curl_init($options['url']);
  
      $method = (isset($options['method'])) ? $options['method'] : 'GET';
  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  
      if (isset($options['auth']) &amp;&amp; $options['auth']) {
          $timestamp = round(microtime(true) * 1000);
          $nonce = $this-&gt;getNonce(11);
          $version = "1.0";
          $signatureMethod = "HMAC-SHA1";
          $signature = $this-&gt;generateSignature($options, $timestamp, $nonce, $signatureMethod, $version);
  
          $authHeader = "Authorization: OAuth oauth_consumer_key=\"{$this-&gt;consumerKey}\",oauth_token=\"{$this-&gt;accessToken}\",oauth_signature_method=\"{$signatureMethod}\",oauth_timestamp=\"{$timestamp}\",oauth_nonce=\"{$nonce}\",oauth_version=\"{$version}\",oauth_signature=\"{$signature}\"";
  
          curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              $authHeader,
              "Content-Type: application/json"
          ));
      }
  
      if (isset($options['body']) &amp;&amp; !empty($options['body'])) {
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($options['body']));
      }
  
      $result = curl_exec($ch);
      curl_close($ch);
  
      return $result;
  }
  </code></pre>
  
  <p>The <code>getNonce</code> method I copied pretty much directly from <a href="https://github.com/BaglerIT/OAuthSimple/blob/master/src/OAuthSimple.php" rel="noreferrer">https://github.com/BaglerIT/OAuthSimple/blob/master/src/OAuthSimple.php</a>. </p>
  
  <p>Here's the method I've written to generate the signature (which has been cobbled together from various SO posts and other sources):</p>
  
  <pre><code>protected function generateSignature($request, $timestamp, $nonce, $signatureMethod, $version)
  {
      $base = $request['method'] . "&amp;" . rawurlencode($request['url']) . "&amp;"
          . rawurlencode("oauth_consumer_key=" . rawurlencode($this-&gt;consumerKey)
          . "&amp;oauth_nonce=" . rawurlencode($nonce)
          . "&amp;oauth_signature_method=" . rawurlencode($signatureMethod)
          . "&amp;oauth_timestamp=" . $timestamp
          . "&amp;oauth_version=" . $version);
  
      $key = rawurlencode($this-&gt;consumerSecret) . '&amp;' . rawurlencode($this-&gt;tokenSecret);
      $signature = base64_encode(hash_hmac('sha1', $base, $key, true));
  
      return $signature;
  }
  </code></pre>
  
  <p>I can actually copy and paste the authorization header from Postman into my <code>$authHeader</code> variable, and replace everything except the timestamp/nonce/signature, and it works. </p>
  
  <p>The response I'm getting from their server right now is <code>[code] =&gt; 0005</code> but I can't find anything in their docs about response codes.</p>
  
  <p>Edit: I had missed looking at the response header - the exact error is invalid signature. </p>