I am working with the Instagram API in magento. I am giving coupons to my instagram followers if they follow our store on Instagram.

I am making the API calls to instagram in PHP using curl. Currently I am wrapping the API calls in helper functions inside my custom module. Should I instead be wrapping these calls in a function inside a model?

For example. I am making an API call to Instagram to determine if the current user is following my account. So, in my controller, I am making a call to my helper function which returns the follow status to my controller. In my controller, I will then update my models if necessary.

Am I correct in putting these API calls inside helper functions? When do I use helpers as opposed to models?


class Company_SocialCoupons_InstagramController extends Mage_Core_Controller_Front_Action
    public function followAction() {

       $status = Mage::helper('socialcoupons/instagram')->getFollow();

       if ($status == 'follows') {

            // 1. ADD DATA TO MY DATABASE using my custom model
            //    - Ex. Mage::getModel('socialcoupons/instagram')->setInstagramId(*IGID*), etc. 
            // 2. CREATE COUPON
            // 3. EMAIL COUPON TO CUSTOMER

class Company_SocialCoupons_Helper_Instagram extends Mage_Core_Helper_Abstract

public function getfollow() {

    $accessToken = $this->getAccessToken();
    $relationshipsUrl = 'https://api.instagram.com/v1/users/' . $this->getUserId() . '/relationship?access_token=' . $accessToken;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $relationshipsUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $jsonData = curl_exec($ch);

    $response = json_decode($jsonData, true);
    $status = $response['data']['outgoing_status'];
    return $status;

public function generateAccessToken($code) {

    // exchange code for access token
    $accessTokenUrl = 'https://api.instagram.com/oauth/access_token';
    $data = array(
        'client_id'     => $this->getClientId(),
        'client_secret' => $this->getClientSecret(),
        'code'          => $code,
        'grant_type'    => 'authorization_code',
        'redirect_uri'  => $this->getRedirectUri()

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $accessTokenUrl);
    curl_setopt($ch, CURLOPT_POST, count($data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $jsonData = curl_exec($ch);

    $response = json_decode($jsonData, true);

    if (isset($response['error_type'])) { // no error

        return $this->_redirect('*/*/authorize');  

    $accessToken = $response['access_token'];
    $id          = $response['user']['id'];
    $username    = $response['user']['username'];


    return array(
        'id'       => $id,
        'username' => $username


4 Answers 4


First you must ask yourself what is the difference between a model and a helper. The most common answer is "a model has a table behind it". Then ask yourself "why are Observers listed as models and not helpers".

Helpers should not exist. But the most common practice is.. when you don't know where to put some code, you put it in a helper.
This is wrong, in my opinion. Using helpers is not really in the OOP spirit. You are just grouping some independent functions inside a class.

But enough philosophical talk.
I would use a model. Mainly because helpers are always singletons. Mage::helper() always returns the same instance of a helper class.
For models you can get new instances and singletons, depending on what you need. So it's a bit more flexible using a model.

But in this specific case, if you only need one instance of your class you can use either a helper or a model. There is no difference. Just what makes you comfortable.

  • Thanks for this. When I create a new model class that is going to simply make APi calls do I need to extend Mage_Core_Model_Abstract or do I not need to extend anything? Nov 2, 2014 at 1:21
  • 3
    You don't need to extend the abstract model. But you could extend Varien_Object. It might be useful but not mandatory
    – Marius
    Nov 2, 2014 at 8:12

I would argue that it is more suited to a model, since its primary purpose is accessing and representing data.



echo $MyModel->getUserName();


echo $MyHelper->getFullname($SomeModelThatImplementsSomeStuff)..

If it has a INTERNAL STATE it's a model. If it doesn't it's a helper with all mathematical correct functions, like sin(x) or str_tolower($text). A Model has a internal state a Helper get's a state injected as a dependancy.


If the methods are used by many classes (blocks/models/controllers) and common between multiple models, then a helper is the obvious choice.

If the methods are only ever used when a single model is instantiated, then within that model is the right place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.