Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

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.