title: Instagram Authentication using PHP
body: <p>Hello fellow developers,</p>
  
  <p>I searched everywhere and it seems like this question remain unresolved, some appears to solve this by using proxy servers or changing their IP address, which didn't work in my case. Since Instagram doesn't have an official PHP or JavaScript SDK, I need to ask this question here.</p>
  
  <p>When I attempt to make a Oauth Call to Instagram by retrieved token it will return an error message below:</p>
  
  <pre><code>{"code": 400, "error_type": "OAuthException", "error_message": "Matching code was not found or was already used."}
  </code></pre>
  
  <p>I have included by code below for your observation:</p>
  
  <h3>Getting Token</h3>
  
  <pre><code>private function getLoginURL($scopes = array("basic")) {
      return self::API_OAUTH_URL."/?client_id=".self::CLIENT_ID."&amp;redirect_uri=".self::REDIRECT_URI."&amp;scope=".implode("+", $scopes)."&amp;response_type=code";
  }
  </code></pre>
  
  <h3>Authentication (Oauth 2.0)</h3>
  
  <pre><code>private function auth() {
      $api_data = array(
          'client_id'     =&gt; self::CLIENT_ID,
          'client_secret' =&gt; self::CLIENT_SECRET,
          'grant_type'    =&gt; 'authorization_code',
          'redirect_uri'  =&gt; urlencode(self::TOKEN_URL),
          'code'          =&gt; self::TOKEN
      );
  
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, self::TOKEN_URL);
      curl_setopt($ch, CURLOPT_POST, count($api_data));
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_data));
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_TIMEOUT, 90);
  
      $json_data = curl_exec($ch);
      curl_close($ch);
      //return json_decode($json_data, true);
      return $json_data;
  }
  </code></pre>
  
  <p>Could someone help me out :(</p>