4

Since facebook requires a website url redirecting to my instagram app I wondered if there is a way to make this work. I tried a code from a youtube tutorial which seems to work on normal webpages perfectly. But i receive mutlitple errors when trying to test it on localhost. I need to do it on localhost because my website will not be online but hosted by a raspberry pi later on.

The webpage contains of the index.php, defines.php and a instagram_basic_api.php

Here the code of those phps:

defines.php (exchanged id and secret here to hide my data):

<?php

    define( 'INSTAGRAM_APP_ID', 'insertid');
    define( 'INSTAGRAM_APP_SECRET', 'insertsecret');
    define( 'INSTAGRAM_APP_REDIRECT_URI', 'https://localhost/test');

?>

instagram_basic_display_api.php:

<?php
    require_once( 'defines.php' );

    Class instagram_basic_display_api {
        private $_appId = INSTAGRAM_APP_ID;
        private $_appSecret = INSTAGRAM_APP_SECRET;
        private $_redirectUrl = INSTAGRAM_APP_REDIRECT_URI;
        private $_getCode = '';
        private $_apiBaseUrl = 'https://api.instagram.com/';
        private $_graphBaseUrl = 'https://graph.instagram.com/';
        private $_userAccessToken = '';
        private $_userAccessTokenExpires = '';

        public $authorizationUrl = '';
        public $hasUserAccessToken = false;
        public $userId = '';

        function __construct( $params ) {
            // save instagram code
            $this->_getCode = $params['get_code'];

            // get an access token
            $this->_setUserInstagramAccessToken( $params );

            // get authorization url
            $this->_setAuthorizationUrl();
        }

        public function getUserAccessToken() {
            return $this->_userAccessToken;
        }

        public function getUserAccessTokenExpires() {
            return $this->_userAccessTokenExpires;
        }

        private function _setAuthorizationUrl() {
            $getVars = array( 
                'app_id' => $this->_appId,
                'redirect_uri' => $this->_redirectUrl,
                'scope' => 'user_profile,user_media',
                'response_type' => 'code'
            );

            // create url
            $this->authorizationUrl = $this->_apiBaseUrl . 'oauth/authorize?' . http_build_query( $getVars );
        }

        private function _setUserInstagramAccessToken( $params ) {
            if ( $params['access_token'] ) { // we have an access token
                $this->_userAccessToken = $params['access_token'];
                $this->hasUserAccessToken = true;
                $this->userId = $params['user_id'];
            } elseif ( $params['get_code'] ) { // try and get an access token
                $userAccessTokenResponse = $this->_getUserAccessToken();
                $this->_userAccessToken = $userAccessTokenResponse['access_token'];
                $this->hasUserAccessToken = true;
                $this->userId = $userAccessTokenResponse['user_id'];

                // get long lived access token
                $longLivedAccessTokenResponse = $this->_getLongLivedUserAccessToken();
                $this->_userAccessToken = $longLivedAccessTokenResponse['access_token'];
                $this->_userAccessTokenExpires = $longLivedAccessTokenResponse['expires_in'];
            }
        }

        private function _getUserAccessToken() {
            $params = array(
                'endpoint_url' => $this->_apiBaseUrl . 'oauth/access_token',
                'type' => 'POST',
                'url_params' => array(
                    'app_id' => $this->_appId,
                    'app_secret' => $this->_appSecret,
                    'grant_type' => 'authorization_code',
                    'redirect_uri' => $this->_redirectUrl,
                    'code' => $this->_getCode
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        private function _getLongLivedUserAccessToken() {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . 'access_token',
                'type' => 'GET',
                'url_params' => array(
                    'client_secret' => $this->_appSecret,
                    'grant_type' => 'ig_exchange_token',
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        public function getUser() {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . 'me',
                'type' => 'GET',
                'url_params' => array(
                    'fields' => 'id,username,media_count,account_type',
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        public function getUsersMedia() {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . $this->userId . '/media',
                'type' => 'GET',
                'url_params' => array(
                    'fields' => 'id,caption,media_type,media_url',
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        public function getPaging( $pagingEndpoint ) {
            $params = array(
                'endpoint_url' => $pagingEndpoint,
                'type' => 'GET',
                'url_params' => array(
                    'paging' => true
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        public function getMedia( $mediaId ) {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . $mediaId,
                'type' => 'GET',
                'url_params' => array(
                    'fields' => 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username'
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        public function getMediaChildren( $mediaId ) {
            $params = array(
                'endpoint_url' => $this->_graphBaseUrl . $mediaId . '/children',
                'type' => 'GET',
                'url_params' => array(
                    'fields' => 'id,media_type,media_url,permalink,thumbnail_url,timestamp,username'
                )
            );

            $response = $this->_makeApiCall( $params );
            return $response;
        }

        private function _makeApiCall( $params ) {
            $ch = curl_init();

            $endpoint = $params['endpoint_url'];

            if ( 'POST' == $params['type'] ) { // post request
                curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params['url_params'] ) );
                curl_setopt( $ch, CURLOPT_POST, 1 );
            } elseif ( 'GET' == $params['type'] && !$params['url_params']['paging'] ) { // get request
                $params['url_params']['access_token'] = $this->_userAccessToken;

                //add params to endpoint
                $endpoint .= '?' . http_build_query( $params['url_params'] );
            }

            // general curl options
            curl_setopt( $ch, CURLOPT_URL, $endpoint );

            curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
            curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

            $response = curl_exec( $ch );

            curl_close( $ch );

            $responseArray = json_decode( $response, true );

            if ( isset( $responseArray['error_type'] ) ) {
                var_dump( $responseArray );
                die();
            } else {
                return $responseArray;
            }
        }
    }

index.php:

<?php
    require_once( 'instagram_basic_display_api.php' );

    $accessToken = 'ACCESS-TOKEN';

    $params = array(
        'get_code' => isset( $_GET['code'] ) ? $_GET['code'] : '',
        'access_token' => $accessToken,
        'user_id' => 'USER-ID'
    );
    $ig = new instagram_basic_display_api( $params );
?>
<meta charset="utf-8">
<h1>Instagram Basic Display API</h1>
<hr />
<?php if ( $ig->hasUserAccessToken ) : ?>
    <h4>IG Info</h4>
    <hr />
    <?php $user = $ig->getUser(); ?>
    <pre>
        <?php print_r( $user ); ?>
    </pre>
    <h1>Username: <?php echo $user['username']; ?></h1>
    <h2>IG ID: <?php echo $user['id']; ?></h2>
    <h3>Media Count: <?php echo $user['media_count']; ?></h3>
    <h4>Account Type: <?php echo $user['account_type']; ?></h4>
    <hr />
    <h3>Highlighted Post</h3>
    <?php $highlightedPostId = 'MEDIA-ID'; ?>
    <div>Media ID: <?php echo $highlightedPostId; ?></div>
    <div>
        <?php
            $media = $ig->getMedia( $highlightedPostId );
            $mediaChildren = $ig->getMediaChildren( $highlightedPostId );
        ?>
        <h4>Raw Data</h4>
        <textarea style="width:100%;height:400px;">
            Media <?php print_r( $media ); ?>
            Children <?php print_r( $mediaChildren ); ?>
        </textarea>
    </div>
    <div style="margin-bottom:20px;margin-top:20px;border:3px solid #333">
        <div>
            <?php foreach ( $mediaChildren['data'] as $child ) : ?>
                <?php if ( 'IMAGE' == $child['media_type'] ) : ?>
                    <img style="height:320px" src="<?php echo $child['media_url']; ?>" />
                <?php else : ?>
                    <video height="240" width="320" controls>
                        <source src="<?php echo $child['media_url']; ?>">
                    </video>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
        <div>
            <b>Caption: <?php echo $media['caption']; ?></b>
        </div>
        <div>
            Posted by: <?php echo $media['username']; ?> at <?php echo $media['timestamp']; ?>
        </div>
        <div>
            Link: <a href="<?php echo $media['permalink']; ?>" target="_blank"><?php echo $media['permalink']; ?></a>
        </div>
        <div>
            ID: <?php echo $media['id']; ?>
        </div>
        <div>
            Media Type: <?php echo $media['media_type']; ?>
        </div>
    </div>
    <?php $usersMedia = $ig->getUsersMedia(); ?>
    <h3>Users Media Page 1 (<?php echo count( $usersMedia['data'] ); ?>)</h3>
    <h4>Raw Data</h4>
    <textarea style="width:100%;height:400px;"><?php print_r( $usersMedia ); ?></textarea>
    <h4>Posts</h4>
    <ul style="list-style: none;margin:0px;padding:0px;">
        <?php foreach ( $usersMedia['data'] as $post ) : ?>
            <li style="margin-bottom:20px;border:3px solid #333">
                <div>
                    <?php if ( 'IMAGE' == $post['media_type'] || 'CAROUSEL_ALBUM' == $post['media_type']) : ?>
                        <img style="height:320px" src="<?php echo $post['media_url']; ?>" />
                    <?php else : ?>
                        <video height="240" width="320" controls>
                            <source src="<?php echo $post['media_url']; ?>">
                        </video>
                    <?php endif; ?>
                </div>
                <div>
                    <b>Caption: <?php echo $post['caption']; ?></b>
                </div>
                <div>
                    ID: <?php echo $post['id']; ?>
                </div>
                <div>
                    Media Type: <?php echo $post['media_type']; ?>
                </div>
                <div>
                    Media URL: <?php echo $post['media_url']; ?>
                </div>
            </li>
        <?php endforeach; ?>
    </ul>
    <?php $usersMediaNext = $ig->getPaging( $usersMedia['paging']['next'] ); ?>
    <h3>Users Media Page 2 (<?php echo count( $usersMediaNext['data'] ); ?>)</h3>
    <h4>Raw Data</h4>
    <textarea style="width:100%;height:400px;"><?php print_r( $usersMediaNext ); ?></textarea>
    <h4>Posts</h4>
    <ul style="list-style: none;margin:0px;padding:0px;">
        <?php foreach ( $usersMediaNext['data'] as $post ) : ?>
            <li style="margin-bottom:20px;border:3px solid #333">
                <div>
                    <?php if ( 'IMAGE' == $post['media_type'] || 'CAROUSEL_ALBUM' == $post['media_type']) : ?>
                        <img style="height:320px" src="<?php echo $post['media_url']; ?>" />
                    <?php else : ?>
                        <video height="240" width="320" controls>
                            <source src="<?php echo $post['media_url']; ?>">
                        </video>
                    <?php endif; ?>
                </div>
                <div>
                    <b>Caption: <?php echo $post['caption']; ?></b>
                </div>
                <div>
                    ID: <?php echo $post['id']; ?>
                </div>
                <div>
                    Media Type: <?php echo $post['media_type']; ?>
                </div>
                <div>
                    Media URL: <?php echo $post['media_url']; ?>
                </div>
            </li>
        <?php endforeach; ?>
    </ul>
<?php else : ?>
    <a href="<?php echo $ig->authorizationUrl; ?>">
        Authorize w/Instagram
    </a>
<?php endif; ?>

some of the error messages (may be the main issues here):

Username: Notice: Undefined index: username in E:\Xampp\htdocs\test\index.php on line 23

IG ID: Notice: Undefined index: id in E:\Xampp\htdocs\test\index.php on line 24

Media Count: Notice: Undefined index: media_count in E:\Xampp\htdocs\test\index.php on line 25

Account Type: Notice: Undefined index: account_type in E:\Xampp\htdocs\test\index.php on line 26

0 Answers0