Wednesday, March 15, 2017

How to get latest images from an instagram account easily

In a web project i've recently been involved in, i needed to get latest images from the instagram account of website owner. As i haven't made this before i've searched and visited many web pages and seen many examples mostly using the instagram api to get latest images of a given instagram account. However i didn't need users login using their own instagram account and show their own images on the website so i thought there must be another way to get publicly shared images from an instagram account. Finally i've seen that despite its not mentioned in instagram api docs there is another simple way without using any api and external code to get latest images from an instagram account.

I've found that you can get latest media of an instagram account as json by getting content of an url in this format:

https://www.instagram.com/[user_id]/media/

Instead of [user_id] you must use the user id of instagram account. The result is a json data which includes many information about latest media from that user on instagram. As i see this url returns latest 20 media publicly shared from the instagram account.

You can use CURL to get contents of this url and parse using json_decode() function in php easily and get information you need from latest images. In my case i only needed image and links so i parsed only these information. Below is the php code i used to show latest instagram images on a web page:

$instagram = new Plugin_Instagram('instagram_account_user_id');
$images = $instagram->getMedia(3); // get only latest 3 images
foreach($images as $p){
// some html code here }

And here is the content of the class Plugin_Instagram i've written to get latest images from an instagram account:

class Plugin_Instagram {
public $user_id = '';
public function __construct($user_id) {
$this->user_id = $user_id;
}
public function getMedia($count=20){
$url = 'https://www.instagram.com/'.$this->user_id.'/media/';
$json = $this->fetchData($url);
$data = json_decode($json);
if(!isset($data->items)){
return array();
}
$return = array(); $i=0;
foreach($data->items as $post){
$return[] = array(
'link'=>$post->link,
'type'=>$post->type, 'img'=>$post->images->thumbnail->url,
'img2'=>$post->images->low_resolution->url,
'img3'=>$post->images->standard_resolution->url,
);
$i++;
if($i>=$count){
break;
}
}
return $return;
}
private function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20); $result = curl_exec($ch);
curl_close($ch);
return $result;
}
}

You can change the return array if you need more information about the latest images from instagram account.


Monday, March 6, 2017

Replace php short tags in all php files with netbeans

I've needed to replace short php tags (<?) in my old php files to standart php tags (<?php) for compatibility on most servers. I've found a regular expression which you can use in netbeans or another IDE/editor to make this change quickly in all your project files. To apply replace first open your netbeans project and click the project name on side menu as a selection of scope. You can select a subfolder too if you want to apply replace limited to scripts in a directory. Then type Ctrl + H to open replace tool window. Enter Containing Text and Replace With values as following:

Containing Text: <\?( )*(?!php)(?!=)(?!xml)(?!mso)
Replace With: <?php

Note that the replace text should contain a space at the end to avoid problems. Also make sure match option is selected as Regular Expression. You can enter File Name Patterns value as *.php if you need.




When ready click Continue to let netbeans find all replaces. If it finds any you should see the results like that:




You can select which results will be replaced by using the checkbox for each file. When ready click the replace button at the bottom to replace php short tags in files.

By applying this replace on netbeans or another IDE which has this feature, you can update short tags to standart php tags which is a better usage. However the usage of <?= ?> style expressions don't need to be replaced as you may have noticed in the regular expression rule we've used.

Sunday, March 5, 2017

Upgrade to Linux Mint 18

I've upgraded one of my computers to Linux Mint 18 from 17 using the steps on this page:

https://community.linuxmint.com/tutorial/view/2316

First i've upgraded to version 17.3 by using the menu on update manager. Than i've started upgrade process to version 18 by using the steps on previous url which took me a long time to complete (possibly bacause of internet connection problems).

I'd like to write some of the changes i see during my usage.

First of all i've noticed that PHP version has been updated to PHP 7 which was something i wanted and that was actually the main reason to upgrade Mint for me. However i'll need to make some changes to my framework to make it compatible for PHP 7.

Another change i've found is about scrolling behaviour which has been reversed in direction. When i scroll down the page content goes up as opposite to the classical behaviour of scroll on pages. But i have changed this to the normal direction by disabling the setting "Natural scrolling" on touchpad settings of control panel.

I've also discovered that there has been a change with text fonts on google chrome which may be caused by default font definitions of Linux Mint on the new version. I can also feel that the terminal font seems to have changed.

On desktop i can't actually notice a change except that texts of icons on desktop have changed color to black so that i cant see them on my previous solid black background. Text color changes to white when i set theme but this happened again the next session so i have changed the background. Let me remind that as in every new version of Linux Mint there are many more desktop images you can select from for your desktop.

You can also realize that some apps have been renamed in Linux Mint. For example Pluma has been replaced by Xed.

---
I'll continue updating this post as i experience new changes or problems i may have so that users who have the same problems may found it useful.