title: Using Curl in PHP
body: <p>I am working on a project, to help me learn how to use <code>curl</code> through <code>PHP</code>. I am attempting to get data from the <a href="https://github.com/justintv/Twitch-API" rel="nofollow"><code>Twitch-API</code></a> using my own account for testing.</p>
  
  <p>I have successfully authenticated my account with my domain by using: </p>
  
  <pre><code>https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&amp;client_id=...&amp;redirect_uri=...&amp;scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription&amp;state=...
  </code></pre>
  
  <p>I have removed <code>client_id</code>, <code>redirect_uri</code> and <code>state</code> to show the link I used.</p>
  
  <p>Once successfully authenticated it returns back to a domain that I specify (<code>redirect_uri</code>), once it gets back to that domain the website only knows the authentication key that is generated once accepted by the user, from twitch.</p>
  
  <p>Example auth: <code>3ofbaoidzkym72ntjua1gmrr66o0nd</code></p>
  
  <p>Now I would like to be able to get the username of the user, <a href="https://github.com/justintv/Twitch-API/blob/master/v3_resources/users.md" rel="nofollow">there is documentation on it</a>:</p>
  
  <pre><code>curl -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Authorization: OAuth &lt;access_token&gt;' \
  -X GET https://api.twitch.tv/kraken/user
  </code></pre>
  
  <p>I am attempting to do this in PHP, but I don't understand the curl functions... Here's what I've got so far:</p>
  
  <pre><code>&lt;?php if(isset($_GET['code']) &amp;&amp; isset($_GET['scope'])) { ?&gt;
  
  &lt;pre&gt;
  &lt;?php
      $auth = $_GET['code'];
      $twitch = curl_init();
  
      $headers = array();
      $headers[] = 'Accept: application/vnd.twitchtv.v3+json';
      $headers[] = 'Authorization: OAuth ' .$auth;
      curl_setopt($twitch, CURLOPT_HEADER, $headers);
      curl_setopt($twitch, CURLOPT_URL, "https://api.twitch.tv/kraken/user");
  
      curl_exec($twitch);
  ?&gt;
  &lt;/pre&gt;
  
  &lt;?php }; ?&gt;
  </code></pre>
  
  <p>When I attempt to run this section of code, I get some errors:</p>
  
  <pre><code>HTTP/1.1 401 Unauthorized
  Server: nginx
  Date: Sat, 08 Aug 2015 13:43:51 GMT
  Content-Type: application/json; charset=utf-8
  Content-Length: 89
  Connection: keep-alive
  Status: 401 Unauthorized
  X-API-Version: 3
  WWW-Authenticate: OAuth realm='TwitchTV'
  Cache-Control: max-age=0, private, must-revalidate
  Vary: Accept-Encoding
  X-UA-Compatible: IE=Edge,chrome=1
  X-Request-Id: 4bc2e0bfadf6817366b4eb19ab5751bf
  X-Runtime: 0.007862
  Accept-Ranges: bytes
  X-Varnish: 1641121794
  Age: 0
  Via: 1.1 varnish
  X-MH-Cache: rails-varnish-5cb970; M
  
  {"error":"Unauthorized","status":401,"message":"Token invalid or missing required scope"}
  </code></pre>
  
  <p>But I am unsure on how to fix this problem as, to me, it seems I am/have done everything that the documentation says to do...</p>
  
  <p>How should I go about fixing this issue?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>It seems to work if I request using my <a href="https://github.com/justintv/Twitch-API/blob/master/v3_resources/users.md#example-request" rel="nofollow">twitch username</a>:</p>
  
  <pre><code>curl -H 'Accept: application/vnd.twitchtv.v3+json' \
  -X GET https://api.twitch.tv/kraken/users/test_user1
  </code></pre>
  
  <p>My Code for using the username:</p>
  
  <pre><code>&lt;?php
  $auth = urlencode($_GET['code']);
  $twitch = curl_init();
  
  $headers = array();
  $headers[] = 'Accept: application/vnd.twitchtv.v3+json';
  #$headers[] = 'Authorization: OAuth ' .$auth;
  curl_setopt($twitch, CURLOPT_HTTPHEADER , $headers);
  curl_setopt($twitch, CURLOPT_URL, "https://api.twitch.tv/kraken/users/...");
  
  curl_exec($twitch);
  ?&gt;
  </code></pre>
  
  <p>But I wouldn't know the user's username unless I get it from the statement which is producing an error and store it in a database.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Reading into the documentation abit more, it requires the scope as well as the access token. I have been able to get this:</p>
  
  <p>Example:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Accept: application/vnd.twitchtv.v3+json
      [1] =&gt; Authorization: OAuth code=scn89zerug002sr6r95z9ngbxmd0d2&amp;scope=user_read+channel_read+channel_subscriptions+user_subscriptions+channel_check_subscription
  )
  </code></pre>
  
  <p>But I still get the error...</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>So I read through the documentation <strong>EVEN MORE</strong> and now I have gotten to this:</p>
  
  <pre><code>class twitch {
      var $base_url = "https://api.twitch.tv/kraken/";
      var $client_id = "...";
      var $client_secret = "...";
      var $return_url = "...";
      var $scope_array = array('user_read','channel_read','channel_subscriptions','user_subscriptions','channel_check_subscription');
  
      public function get_access_token($code,$state) {
          $ch = curl_init($this-&gt;base_url . "oauth2/token");
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_POST, 1);
          $fields = array(
               'client_id' =&gt; $this-&gt;client_id,
               'client_secret' =&gt; $this-&gt;client_secret,
               'grant_type' =&gt; 'authorization_code',
               'redirect_uri' =&gt; $this-&gt;redirect_url,
               'code' =&gt; $code
          );
          curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
          $data = curl_exec($ch);
          $response = json_decode($data, true);
          curl_close($ch);
  
          echo "&lt;pre&gt;".print_r($this-&gt;redirect_url,true)."&lt;/pre&gt;";
          echo "&lt;pre&gt;".print_r($response,true)."&lt;/pre&gt;";
  
          return $response["access_token"];
      }
  };
  
  $auth = new twitch();
  print_r($auth-&gt;get_access_token($_GET['code'],$_GET['state']));
  </code></pre>
  
  <p>But this time there is <strong>another</strong> error, saying that my <code>'redirect_uri' =&gt; $this-&gt;redirect_url</code> is different to the one which is held by twitch.</p>
  
  <pre><code>Array
  (
      [error] =&gt; Bad Request
      [status] =&gt; 400
      [message] =&gt; Parameter redirect_uri does not match registered URI
  )
  </code></pre>
  
  <p>I have even copied and pasted from the twitch website to my variable and the other way around, I still get the same error... Now I'm even more stuck, but at least a step closer.</p>