instagram subscription tag steps on Real-time Photo Updates

476 views Asked by At

I'm new in instagram, base on Real-time Photo Updates. Don't understand the process, can anyone enlighten me how to create callback URL in php? From the register client's redirect uri need to link up to this callback URL? The below code is how i did for the callback URL

$checkin_url = "https://api.instagram.com/v1/subscriptions/";

//$instagram[] for client_id, client_secret, redirect_uri
$parameters = array(
    'client_id' => $instagram['client_id'],
    'client_secret' => $instagram['client_secret'],
    'object' => 'tag',
    'aspect' => 'media',
    'object_id' => 'nofilter',
    'callback_url' =>  $instagram['redirect_uri']
);

$curl = curl_init($checkin_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);

print($response);
2

There are 2 answers

0
Tmb On

You missed an important step in the subscription process (It's in the doc):

When you POST with the info above to create a new subscription, we simultaneously submit a GET request to your callback URL [...] In order to verify the subscription, your server must respond to the GET request with the hub.challenge parameter only:

In other word, when you create the subscription, they send a GET request to your callback URI with some parameters, and you have to return the hub.challenge parameters (hub_challenge in PHP, because with PHP the parameters with a . are converted to _).

So in your callback URI, you have to test the request method. If it's GET, do the following :

exit($_GET['hub_challenge']);

and if it's POST, it's a subscription update, so you have to json decode the raw body to get the datas, as stated in the doc' :)

0
Iverson On

Hi Thomas, I got an error and have no idea why it isn't working...

string(92) "{"meta":{"error_type":"APISubscriptionError","code":400,"error_message":"Invalid response"}}"

From the subscription:

$callback_url = "http://myhost.com/auth/instagram/callback";

    $params = array(
        'client_id' => $ig_client_id,
        'client_secret' => $ig_client_secret,
        'aspect' => "media",
        'object' => "tag",
        'object_id' => "greentea",
        'callback_url' => $callback_url
    );

    $defaults = array(
        CURLOPT_URL => 'https://api.instagram.com/v1/subscriptions/',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array('Accept: application/json')
    );
    $ch = curl_init();
    curl_setopt_array($ch, $defaults);
    $jsonData = curl_exec($ch);
    curl_close($ch);
    var_dump($jsonData);

From the callback:

if (isset ($_GET['hub_challenge'])){
    exit($_GET['hub_challenge']);
}
else {
    $rawdata = file_get_contents('php://input');
    $decode_data = json_decode($rawdata);
    echo $decode_data;
}