GithubHelp home page GithubHelp logo

instagram's Introduction

Instagram API

index.php

<?php
include 'Instagram.php';

$instagram = new Instagram();
$instagram->setClientId('');
$instagram->setClientSecret('');
$instagram->setRedirectUri('');

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $instagram->setResponseType($_POST['type']);
    $instagram->authorize();
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Instagram API</title>
<style>.item{width: 150px;display: inline-block;}img{width: 100%;height: auto;}</style>
</head>
<body>
Authorize
<form action="" method="post">
    <button type="submit" name="type" id="ImplicitOAuth" value="code">Implicit OAuth</button>
    <button type="submit" name="type" id="ExplicitOAuth" value="token">Explicit OAuth</button>
</form>

Access token
<form action="" method="GET">
<input type="text" name="access_token" id="access_token" value="<?php echo empty($_GET['access_token'])?'':$_GET['access_token'] ?>">
<button type="submit" id="btn_access_token">Get Access token from hash tag</button>
</form>
<script>
function removeLocationHash(){
    var noHashURL = window.location.href.replace(/#.*$/, '');
    window.history.replaceState('', document.title, noHashURL) 
}
if(window.location.hash) {
    var hash = window.location.hash.substring(14); //Puts hash in variable, and removes the # character
    var name = window.location.hash.substring(1, 13); //Puts hash in variable, and removes the # character
    document.getElementById('access_token').value = hash;
    if(name = "access_token") {
        var path = window.location.href.substr(0, window.location.href.indexOf('#'))
        document.getElementById("btn_access_token").click();
        // window.location.href = path;
    }
}
removeLocationHash();
</script>
<pre>
<?php
if (!empty($_GET['access_token'])){
    $instagram = new Instagram();
    $instagram->setAccessToken($_GET['access_token']);
    $media = $instagram->getMediaRecent();
    foreach ($media as $v) {
        echo '<div class="item"><a href="'.$v['link'].'" target="_blank"><img src="'.$v['thumbnail']['url'].'" alt="'.$v['alt'].'"></a></div>';
    }
}
?>
</body>
</html>

callback.php

include 'Instagram.php';
if (!empty($_GET['code'])){
    $instagram = new Instagram();
    $instagram->setClientId('');
    $instagram->setClientSecret('');
    $instagram->setRedirectUri('');

    $instagram->setCode($_GET['code']);
    $result = $instagram->getAccessToken();
    $result = json_decode($result, true);
    if ( !isset($result['error_message']) ) {
        $access_token = $result['access_token'];
        // $user = $result['user'];
        // $user['id'];
        // $user['username'];
        // $user['profile_picture'];
        // $user['full_name'];
        // $user['bio'];
        // $user['website'];
        // $user['is_business'];
        print_r($result);
        header("location: index.php/?access_token=".$access_token );
    }else{
        print_r($result['error_message']);
    }
}else{
    header("location: index.php" );
}

instagram's People

Contributors

phuquang avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.