We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23849
    • 223 Posts
    Hi All,

    I’ve recently decided to try and feed in tumblr blog posts into some modx sites. I found the following PHP class which seems to do the trick quite well:

    http://code.google.com/p/phptumblr/

    I have this setup on a site, outside of a modx install, and it’s working just fine. However, in attempting to have it work inside modx it produces and error. Here’s what I tried: I created a snippet and inserted the code from the phptumblr class example script (the one that works outside of modx). It includes a couple of files so I adjusted the paths for those includes accordingly. Here’s the snippet code (not the whole story but at least part):

    <?php
    # ***** BEGIN LICENSE BLOCK *****
    # This file is part of phpTumblr.
    # Copyright (c) 2006 Simon Richard and contributors. All rights
    # reserved.
    #
    # phpTumblr is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; either version 2 of the License, or
    # (at your option) any later version.
    # 
    # phpTumblr is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    # 
    # You should have received a copy of the GNU General Public License
    # along with phpTumblr; if not, write to the Free Software
    # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    #
    # ***** END LICENSE BLOCK *****
    
    /*  Execution time counter */
    $nStartTime = microtime(true);
    
    /*  First, you have to include some files :
    /     * The Clearbricks _common.php file
    /     * The Tumblr class itself
    /     * The UTF8 HTML Decode class (used to clean up some non-standards entities)
    /*/
    require 'assets/snippets/phpTumblr/clearbricks/_common.php';
    require 'assets/snippets/phpTumblr/class.read.tumblr.php';
    
    /*  Now you can initiate the Tumblr object with the ID of the Tumblelog you want read from as param.
    /
    /   function __construct($sTumblrID = null,$sHTTPUserAgent = 'phpTumblr')
    /      * Initialize the Tumblr object, take a Tumblr ID as param.
    /      * Optionally take a second param, a string that will be the HTTP User Agent used for the requests made to the API.
    /*/
    $oTumblr = new readTumblr('jmarshvt');
    
    /*  Now, it's time to do some requests from this API. This code will request, in the order:
    /      * 3 video posts
    /      * All regular posts
    /      * The posts with the ID 39185133
    /
    /   function getPosts($nStart = 0,$mNum = 20,$sType = null,$nID = null)
    /      * Request $mNum $sType posts starting from $nStart.
    /      * Take posts of all types if $sType = null.
    /
    /   OR
    /      * If $mNum = 'all', get all $sType posts starting from $nStart
    /      * Take posts of all types if $sType = null.
    /
    /   OR
    /      * If $nID is given, request the post with the ID $nID.
    /*/
    //$oTumblr->getPosts(0,3,'video');
    $oTumblr->getPosts(null,'all','null');
    //$oTumblr->getPosts(null,null,null,36624807);
    
    /*  You're quite done! Now, you can get the array that contain the result.
    /
    /   function dumpArray($bChrono = false,$bDebug = false)
    /      * Sort the array in chronological order if $bChrono is true, inverse if false.
    /      * Return the array-formated content of the requests made to the API.
    /      * If $bDebug = true, return raw decoded JSON from the last request in ['temp'] key of the array. This option was mainly created to help me while dev.
    /*/
    $aTumblr = $oTumblr->dumpArray();
    
    /*  Execution time counter */
    $nEndTime = microtime(true);
    $nExecTime = $nEndTime - $nStartTime;
    if ($aTumblr['stats']['num-inarray'] > 0) { $nExecTimePerPost = $nExecTime / $aTumblr['stats']['num-inarray']; } else { $nExecTimePerPost = 0; }
    
    header('Content-Type: text/html; charset=utf-8');
    echo $aTumblr['stats']['num-inarray'].' posts parsed in '.$nExecTime.'s, that means '.$nExecTimePerPost.'s per post !'."\n\n";
    
    //print_r($aTumblr);
    
    /*$i = 0;
    while ($i < count($aTumblr['posts'])){
    	
    	$postTime = $aTumblr['posts'][$i]['time'];
    	$postID = $aTumblr['posts'][$i]['id'];
    	
    	echo '<p>' . $aTumblr['posts'][$postTime|$postID]['content']['title'] . '</p>';
    	
    	$i++;
    }*/
    
    foreach ($aTumblr['posts'] as $key => $array)
    {
    	print '<h3>'.$array['content']['title'].'</h3>';
    	print $array['content']['body'];
    }
    
    
    /*  Important note : you can do as much getPosts request as you like!
    /   It's impossible to have several times the same post in the array (array key composed with post's id and post's timestamp).
    /*/
    ?>


    When I call the snippet here’s what happens:

    http://blueskiesbelow.com/news.html

    For some reason one of the included files appears to not be working properly (can’t find a class that is definitely there). I’ve never created a snippet that had this much going on around it so I’m probably missing something.

    Can anyone think of why this would work outside of modx but not within? Let me know!
      Nick Hoag
      Creative Partner
      The FutureForward

      http://thefutureforward.com
      • 4041
      • 788 Posts
      Sometimes I’ve found that you have to add MODX_BASE_PATH to file paths on includes and requires for things to work right:

      require MODX_BASE_PATH.'assets/snippets/phpTumblr/clearbricks/_common.php';
      require MODX_BASE_PATH.'assets/snippets/phpTumblr/class.read.tumblr.php';


      </shotinthedark>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 23849
        • 223 Posts
        Hey Breezer,

        Thanks for the tip. I tossed that into the snippet code but it’s still giving me the same error. The 2 ’requires’ in the snippet itself seem to be working - it’s getting to the initial required file and then hitting an error in a file that that file requires. Perhaps I need to include the modx api in some way in the other files being included and add "MODX_BASE_PATH." to those... not sure.
          Nick Hoag
          Creative Partner
          The FutureForward

          http://thefutureforward.com