Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Including php into template page

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3423 Views
  • LoopyLarry
    Avatar
    Community Member
    4 Posts

    Including php into template page Link to this post

    Ok, so this question might be a bit tiresome, but my php experience is very limited (for now!), and I just can't get my head around what I need to do here.

    Here's the situation:

    I want to integrate this - http://blog.kno.at/tools/one-random-photo-from-your-flickr-stream/ - a function for displaying a single random Flickr image, onto my SilverStripe homepage. It's fairly straightforward and I've confirmed it works on my server by running it on a test php page on its own.

    However, getting it to display on Silverstripe is another matter. As I understand it, I can't just chuck a php include into Page.ss, but this is where I get derailed. Between Page.ss, Page.php and layout/page.ss I just can't figure out how to get the php to include. And I suspect the problem is simply that I am flying almost blind with my lack of php knowledge.

    Can anyone advise me how to get this going? Much appreciated, thanks

  • markguinn
    Avatar
    Community Member
    13 Posts

    Re: Including php into template page Link to this post

    In Page.php, add the following function to the Page_Controller class:

    function RandomFlickerPhoto(){
    ob_start();

    // be sure to swap out this url with your own badge
    $badge = 'http://www.flickr.com/badge_code_v2.gne?count=1&display=random&size=m&layout=x&source=user&user=7353617%40N04';
    $width = 400;
    $height = 240;
    $gray = true;
    // you should be able to place this file in the same directory as Page.php
    include_once('flickpic.php');

    $str = ob_get_contents();
    ob_end_clean();
    return $str;
    }

    Then in your template you can just use $RandomFlickerPhoto anywhere you want the photo to show up. The 'Page.ss' in the templates directory is your outer template (header, footer, etc.) and the one in the Layouts directory is the inner template for the guts of a page.

    Hope that helps. I haven't actually tried it so it may take a little tweaking.
    Mark

  • LoopyLarry
    Avatar
    Community Member
    4 Posts

    Re: Including php into template page Link to this post

    Cheers mate! I suspect it is working, but now I'm encountering a lovely error from flickpic.php (which I didn't encounter in my test run).

    [Notice] Undefined variable: user
    GET /

    Line 12 in /home/mwgd/public_html/mysite/code/flickpic.php
    Source

    3 * randomFlickr.class.php usage sample
    4 */
    5
    6 require_once('randomFlickr.class.php');
    7
    8 if ( !isset($_GET['imageUrl']) )
    9 {
    10 try
    11 {
    12 $x = new randomFlickr($user);
    13 if ( $badge )
    14 {
    15 $x->setBadgeUrl($badge);
    16 }
    17 $x->fetch();
    18 }

    At no http://blog.kno.at/tools/one-random-photo-from-your-flickr-stream/ the author comments

    In line 12 the randomFlickr-object is being created using a variable $user. This variable isn’t specified here, therefore will just be ignored. Then we check if a badge-url has been specified in $badge. If so, we change the object’s badge url according.

    A few words about the fetch()-method. It will check if there’s a user id specified (the ugly flickr user-id string, that is, due the badges don’t seem to work with the usernames). If so, it uses the default badge-url for 1 random image and passes that user-id. Easy. If there’s no user specified, the badge-url will be used as-is.

    But for some reason SS won't happily ignore it as he suggests? So as a stab in the dark, in Page.php I add

    $user = 44137955;

    But then the new error is this:

    [Notice] Undefined offset: 0
    GET /

    Line 282 in /home/mwgd/public_html/mysite/code/randomFlickr.class.php
    Source

    273 $sBadge = $this->curlAccess($this->sBadgeUrl . ( $user ? "&user=$user" : '' ));
    274 }
    275 else
    276 {
    277 throw new Exception('allow_url_fopen disabled and CURL not found. Cannot access remote url '. $this->sBadgeUrl . ( $user ? "&user=$user" : '' ));
    278 }
    279
    280 preg_match(self::REGEX_SRC, $sBadge, $matches);
    281
    282 $this->sSrc = $matches[0] . '.jpg';
    283 $this->iId = $matches[1];
    284
    285 preg_match(self::REGEX_URL, $sBadge, $matches);
    286 $this->sUrl = $matches[1];
    287
    288 preg_match(self::REGEX_TITLE, $sBadge, $matches);

    Any ideas on this? (apologies if I'm missing anything obvious...and also don't worry if you can't be bothered with this troubleshooting!)

  • markguinn
    Avatar
    Community Member
    13 Posts

    Re: Including php into template page Link to this post

    Yeah, Silverstripe by default ups the error reporting to include php notices (like unitialized variables). The easiest way to solve that is to add:

    $user = false;

    That would give you the normal functionality without triggering an error. I suspect you might have other issues going on though. What kind of hosting are you using? It looks to me like the curl call could be failing.

  • LoopyLarry
    Avatar
    Community Member
    4 Posts

    Re: Including php into template page Link to this post

    Alas, $user = false; just returns the same error as before

    [Notice] Undefined offset: 0
    GET /

    Line 282 in /home/mwgd/public_html/mysite/code/randomFlickr.class.php
    Source

    273 $sBadge = $this->curlAccess($this->sBadgeUrl . ( $user ? "&user=$user" : '' ));
    274 }
    275 else
    276 {
    277 throw new Exception('allow_url_fopen disabled and CURL not found. Cannot access remote url '. $this->sBadgeUrl . ( $user ? "&user=$user" : '' ));
    278 }
    279
    280 preg_match(self::REGEX_SRC, $sBadge, $matches);
    281
    282 $this->sSrc = $matches[0] . '.jpg';
    283 $this->iId = $matches[1];
    284
    285 preg_match(self::REGEX_URL, $sBadge, $matches);
    286 $this->sUrl = $matches[1];
    287
    288 preg_match(self::REGEX_TITLE, $sBadge, $matches);

    Trace

    * randomFlickr->fetch()
    Line 17 of flickpic.php
    * include_once(/home/mwgd/public_html/mysite/code/flickpic.php)
    Line 53 of HomePage.php
    * HomePage_Controller->RandomFlickerPhoto()
    * call_user_func_array(Array,Array)
    Line 408 of ViewableData.php
    * ViewableData->XML_val(RandomFlickerPhoto,,1)
    Line 119 of .cache.home.mwgd.public_html.themes.pixelgreen.templates.Layout.HomePage.ss
    * include(/tmp/silverstripe-cache-home-mwgd-public_html/.cache.home.mwgd.public_html.themes.pixelgreen.templates.Layout.HomePage.ss)
    Line 357 of SSViewer.php
    * SSViewer->process(HomePage_Controller)
    Line 349 of SSViewer.php
    * SSViewer->process(HomePage_Controller)
    Line 172 of Controller.php
    * Controller->handleAction(HTTPRequest)
    Line 129 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 119 of Controller.php
    * Controller->handleRequest(HTTPRequest)
    Line 29 of ModelAsController.php
    * ModelAsController->handleRequest(HTTPRequest)
    Line 44 of RootURLController.php
    * RootURLController->handleRequest(HTTPRequest)
    Line 277 of Director.php
    * Director::handleRequest(HTTPRequest,Session)
    Line 121 of Director.php
    * Director::direct(/)
    Line 118 of main.php

    etc. etc.

    My hosting is from hostgator.com , using Curl 7.12.1 if that is any help.

    3423 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.