Skip to content

Instantly share code, notes, and snippets.

@henriqueboaventura
Created November 20, 2014 17:45
Show Gist options
  • Save henriqueboaventura/debcd2a5e3b204aaea1e to your computer and use it in GitHub Desktop.
Save henriqueboaventura/debcd2a5e3b204aaea1e to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Instagram extends Controller {
public $instagram_config = null;
public function before()
{
parent::before();
$this->instagram_config = Kohana::$config->load('instagram');
}
public function action_update()
{
//por tag
$url_request = sprintf("https://api.instagram.com/v1/tags/3yz/media/recent?client_id=%s", $this->instagram_config->get('client_id'));
$this->_parse_response($url_request);
//por location
$url_request = sprintf("https://api.instagram.com/v1/locations/60011/media/recent?client_id=%s", $this->instagram_config->get('client_id'));
$this->_parse_response($url_request);
$this->response->headers('content-type', 'application/json');
return $this->response->body(json_encode(array('success' => TRUE, 'last_update' => date('d/m/Y H:i:s'))));
}
protected function _parse_response($url_request)
{
$response = Request::factory($url_request)->execute();
$json_data = json_decode($response->body());
if (isset($json_data) AND !empty($json_data))
{
foreach ($json_data->data as $photo_data)
{
$photo = ORM::factory('photo', array('instagram_id' => $photo_data->id));
if (!$photo->loaded())
{
$photo->values(array(
'from' => $photo_data->user->username,
'low_resolution' => $photo_data->images->low_resolution->url,
'thumbnail' => $photo_data->images->thumbnail->url,
'standard_resolution' => $photo_data->images->standard_resolution->url,
'caption' => isset($photo_data->caption) ? $photo_data->caption->text : NULL,
'instagram_url' => $photo_data->link,
'instagram_id' => $photo_data->id,
'location' => $photo_data->location ? $photo_data->location->name : NULL,
'created_time' => date('Y-m-d H:i:s', $photo_data->created_time)
))->save();
foreach ($photo_data->tags as $tag_data)
{
$tag = ORM::factory('tag', array('name'=> $tag_data));
if (!$tag->loaded())
{
$tag->values(array('name' => $tag_data))->save();
}
$photo->add('tags', $tag);
}
}
}
}
}
public function action_create_subscription()
{
$api_url = 'https://api.instagram.com/v1/subscriptions/';
$request = Request::factory($api_url)
->method('POST')
->post('client_id', $this->instagram_config->get('client_id'))
->post('client_secret', $this->instagram_config->get('client_secret'))
->post('verify_token', 'aaa123')
->post('object', 'tag')
->post('aspect', 'media')
->post('object_id', '3yz')
->post('callback_url', Url::base(TRUE) . 'instagram/subscription_callback')
->execute();
$this->request->headers('content-type', 'application/json');
echo $request->body();
exit;
}
public function action_subscription_callback()
{
if ($this->request->query('hub_mode') == "subscribe")
{
echo $this->request->query('hub_challenge');
}
if ($this->request->method() == Request::POST)
{
return $this->request->redirect(Url::base(TRUE).'instagram/update');
}
}
} // End Application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment