Source

php-bootstrap-boilerplate / inc / instagram.php

Full commit
<?php
class Instagram extends F3instance {

function startswith4($haystack, $needle) {
    return strpos($haystack, $needle) === 0;
}

function pull_data(){
	$this->set('title','PostBox - Instagram Data Pull');
	$instagram_api_clinet_id = 'b9d4b604105648168c671293d10cc67e';
	$instagram_api_url = 'https://api.instagram.com/v1/tags/openbangalore/media/recent?client_id='.$instagram_api_clinet_id;
	$data_pull_messages = array();

	$ch = curl_init ($instagram_api_url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
	$json=curl_exec($ch);
	curl_close ($ch);
	$data = json_decode ($json,true);
	//var_dump($data);
	if($data['meta']['code']==200){
	   //echo 'sucess';
	   $picture_dicts = $data['data'];
	   foreach($picture_dicts as $pic){
		    $tags 			= implode(', ', $pic['tags']);
		    $pincode 		= 0;
		    foreach($pic['tags'] as $tag){
		    	if($this->startswith4($tag, 'pin')){
		    		$pincode = substr($tag,3,10);
		    	}
		    }
		    $lat 			= $pic['location']['latitude'];
		    $lan 			= $pic['location']['longitude'];
		    $created_time 	= $pic['created_time'];
		    $picture_url 	= $pic['images']['standard_resolution']['url'];
		    $post_id 		= $pic['id'];
		    $username 		= $pic["user"]["username"];
	        $website 		= $pic["user"]["website"];
	        $caption		= $pic["caption"]["text"];
			//check if post_id exists, if yes then go to next one. else insert
			$data_pull_messages[] = "Processing the post_id=".$post_id;
			$q = 'select count(*) as count_posts from post_box where post_id="'.$post_id.'"';
			$POSTBOX_DB=F3::get('POSTBOX_DB');
			$POSTBOX_DB->sql($q);
			//print '\n'.$q; 
			$count_posts = 0;
			foreach (F3::get('POSTBOX_DB->result') as $row){
	        	$count_posts = $row['count_posts'];

	        }
	        if ($count_posts == 0){
	        	$data_pull_messages[] = "Lets INSERT.";
	        	$i = 'insert into post_box( post_id , picture_url , tags , lat , lan , created_time , username , website,pincode, caption) values('.'"'.$post_id .'","'.$picture_url .'","'.$tags .'","'.$lat .'","'.$lan .'","'.$created_time .'","'.$username .'","'.$website .'","'.$pincode .'","'.$caption .'"'.')';
	        	//print $i;
	        	$POSTBOX_DB->sql($i);
	        }else{
	        	$data_pull_messages[] =  "Already exists.";
	        }

		}
	}
	$this->set('LANGUAGE','en-US');
	$this->set('sub','sub_data_pull.html');
	$this->set('data_pull_messages',$data_pull_messages);
	$out=$this->render('basic/layout.html');
	$this->set('sub_out_put',$out);
	$this->set('LANGUAGE','en-US');
	echo $this->render('basic/main.html');
}

}//calss end
?>