title: Instagram Like API. Posting a like through curl and php
body: <p>I am trying to figure out how to POST a like for Instagram. Click here to view my website for testing this API.</p>
  
  <p><a href="http://ewands.no-ip.biz/Intern/guangjian/instagram/" rel="nofollow">http://ewands.no-ip.biz/Intern/guangjian/instagram/</a> </p>
  
  <p>This is my first time trying the Instagram API. Google it but no tutorial is found on this topic. Below are my PHP code.</p>
  
  <p>Getting the media id:</p>
  
  <pre><code>$api = file_get_contents("http://api.instagram.com/oembed?url=http://instagram.com/p/n5DyHnKWoo");      
  $apiObj = json_decode($api,true);      
  $media_id = $apiObj['media_id'];
  </code></pre>
  
  <p>After the like button is clicked, the URL becomes <a href="http://ewands.no-ip.biz/Intern/guangjian/instagram?api=like" rel="nofollow">http://ewands.no-ip.biz/Intern/guangjian/instagram?api=like</a>, so from PHP we run the code below:</p>
  
  <pre><code>if($_GET['api'] == 'like'){
  
  header('location:https://api.instagram.com/oauth/authorize/?client_id=3548d38f15b54dbfb744bf0705fa8198&amp;redirect_uri=http://ewands.no-ip.biz/intern/guangjian/instagram?function=like&amp;response_type=code');
  </code></pre>
  
  <p>The above will redirect the page to <a href="http://ewands.no-ip.biz/intern/guangjian/instagram/?function=like&amp;code=3b50fdbcd0184f979b31f5b0d12354c4" rel="nofollow">http://ewands.no-ip.biz/intern/guangjian/instagram/?function=like&amp;code=3b50fdbcd0184f979b31f5b0d12354c4</a> &lt;-- Instagram server added a random code, next, use curl to get the access token:</p>
  
  <pre><code>} 
  if($_GET['function'] == 'like') {
  
      $code = $_GET['code'];
      $url = "https://api.instagram.com/oauth/access_token";
      $access_token_parameters = array(
          'client_id'                =&gt;     '3548d38f15b54dbfb744bf0705fa8198',
          'client_secret'            =&gt;     '97e12a30416d4436a52107d0d1820e91',
          'grant_type'               =&gt;     'authorization_code',
          'redirect_uri'             =&gt;     'http://ewands.no-ip.biz/intern/guangjian/instagram?function=like', 
          'code'                     =&gt;     $code
          );
      $curl = curl_init($url);  
      curl_setopt($curl,CURLOPT_POST,true);   
      curl_setopt($curl,CURLOPT_POSTFIELDS,$access_token_parameters);   
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
      $result = curl_exec($curl);   
      curl_close($curl); 
  
      $arr = json_decode($result,true);
      $token = $arr['access_token'];
  </code></pre>
  
  <p>I have tested that I am able to get the token, so far so good, then the next part does not work, I am just trying my luck, since I am not familiar with curl and there are no tutorial out there from Googling for Instagram API tutorial.</p>
  
  <pre><code>    $url = 'https://api.instagram.com/v1/media/'.$media_id.'/likes';
  
      $curl = curl_init($url);
      curl_setopt($curl,CURLOPT_POST,true);
      curl_setopt($curl,CURLOPT_POSTFIELDS,'access_token='.$token);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      $result = curl_exec($curl);
  
  }
  </code></pre>