Complex HTML in Sidebar (Not Working)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sytyregistry
    New Member
    • Apr 2017
    • 17
    • 5.2.x

    Complex HTML in Sidebar (Not Working)

    I'm trying to add a piece of code to the sidebar to allow the latest Instagram image to auto-populate. I have the code functioning in a sample PHP file on the server and it performs properly. However, if I add in the code to an HTML module or PHP module for the sidebar, it breaks (starts spewing raw code in place of the desired image). I've gotten the proper token from Instagram and setup up the Instagram API properly (again, it works in a standalone PHP file).

    Any ideas?

    Here's the properly working example - https://sytyregistry.com/test/

    Here's the code I've created:

    Code:
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>ISTR Instagram</title>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
        </head>
    
        <body>
        <?php
            $username = 'internationalsytyregistry'; // your username
            $access_token = '3123983982.a95f87e.04c537ee26a7491fb15a978f6e572e48'; // put your access token here
            $count = 1; // number of images to show
    
        class InstaWCD{
    
    
        function userID(){
            $username = strtolower($this->username); // sanitization
            $token = $this->access_token;
            $url = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$token;
            $get = file_get_contents($url);
            $json = json_decode($get);
    
            foreach($json->data as $user){
                if($user->username == $username){
                    return $user->id;
                }
            }
    
            return '00000000'; // return this if nothing is found
        }
    
        function userMedia(){
            $url = 'https://api.instagram.com/v1/users/'.$this->userID().'/media/recent/?access_token='.$this->access_token;
    
            $content = file_get_contents($url);
              return $json = json_decode($content, true);
        }
    
        }
        $insta = new InstaWCD();
            $insta->username = $username;
            $insta->access_token = $access_token;
    
            $ins_media = $insta->userMedia();
            $i = 0;
            foreach ($ins_media['data'] as $vm):
                if($count == $i){ break;}
                $i++;
                $img = $vm['images']['low_resolution']['url'];
                $link = $vm["link"];
        ?>
    
                <a target="_blank" href="<?php echo $link ?>">
                    <img src="<?php echo $img; ?>" width="250" style="float:left;" /> <!-- Adjust width to suit / Add height if necessary -->
                </a>
    
        <?php endforeach ?>
    
    </body>
    </html>
  • glennrocksvb
    Former vBulletin Developer
    • Mar 2011
    • 4021
    • 5.7.X

    #2
    Your code should not include html, head and body tags as the site already has them. And you should use PHP module and exclude <?php ?> tags.

    Flag Icon Postbit Insert GIPHY Impersonate User BETTER INITIALS AVATAR Better Name Card Quote Selected Text Bookmark Posts Post Footer Translate Stop Links in Posts +MORE!

    Comment


    • sytyregistry
      sytyregistry commented
      Editing a comment
      Doh! Thanks so much Glenn. Totally overthought the matter.

Related Topics

Collapse

Working...