Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. <?php
  2.  
  3. class Company_SocialCoupons_InstagramController extends Mage_Core_Controller_Front_Action
  4. {
  5. public function followAction() {
  6.  
  7. $status = Mage::helper('socialcoupons/instagram')->postFollow();
  8.  
  9. if ($status == 'follows') {
  10.  
  11. // 1. ADD DATA TO MY DATABASE using my custom model
  12. // - Ex. Mage::getModel('socialcoupons/instagram')->setInstagramId(*IGID*), etc.
  13. // 2. CREATE COUPON
  14. // 3. EMAIL COUPON TO CUSTOMER
  15. }
  16. }
  17.  
  18. class Company_SocialCoupons_Helper_Instagram extends Mage_Core_Helper_Abstract
  19. {
  20.  
  21. public function getfollow() {
  22.  
  23. $accessToken = $this->getAccessToken();
  24. $relationshipsUrl = 'https://api.instagram.com/v1/users/' . $this->getUserId() . '/relationship?access_token=' . $accessToken;
  25.  
  26. $ch = curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $relationshipsUrl);
  28. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30. $jsonData = curl_exec($ch);
  31. curl_close($ch);
  32.  
  33. $response = json_decode($jsonData, true);
  34. $status = $response['data']['outgoing_status'];
  35. return $status;
  36. }
  37.  
  38. public function generateAccessToken($code) {
  39.  
  40. // exchange code for access token
  41. $accessTokenUrl = 'https://api.instagram.com/oauth/access_token';
  42. $data = array(
  43. 'client_id' => $this->getClientId(),
  44. 'client_secret' => $this->getClientSecret(),
  45. 'code' => $code,
  46. 'grant_type' => 'authorization_code',
  47. 'redirect_uri' => $this->getRedirectUri()
  48. );
  49.  
  50. $ch = curl_init();
  51. curl_setopt($ch, CURLOPT_URL, $accessTokenUrl);
  52. curl_setopt($ch, CURLOPT_POST, count($data));
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  54. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  56. $jsonData = curl_exec($ch);
  57. curl_close($ch);
  58.  
  59. $response = json_decode($jsonData, true);
  60.  
  61. if (isset($response['error_type'])) { // no error
  62.  
  63. Mage::getSingleton('core/session')->unsInstagramAccessToken();
  64. Mage::getSingleton('core/session')->addError($response['error_message']);
  65. return $this->_redirect('*/*/authorize');
  66. }
  67.  
  68. $accessToken = $response['access_token'];
  69. $id = $response['user']['id'];
  70. $username = $response['user']['username'];
  71.  
  72. Mage::getSingleton('core/session')->setInstagramAccessToken($accessToken);
  73.  
  74. return array(
  75. 'id' => $id,
  76. 'username' => $username
  77. );
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement