Jump to content

OOP HELP


Danny620

Recommended Posts

I have written an object that connects to an api and pulls off the results that bits working fine however ive tried to add another function that will save the json results from the api to file and instead of querying api it looks for file, it does not seem to be returning results or writing them to file?

 

<?php

/// Replace '*********' With your api key which can be obtained from http://www.socialnewsoffice.com
DEFINE('api_key', 'b5cbd66dca99c49d1a6c3d');

DEFINE('API_URL', 'http://www.socialnewsoffice.com/');

DEFINE('CACHE_FILE_DIRECTORY', $_SERVER['DOCUMENT_ROOT'] . 'cache/');

//check if you have curl loaded
if (!function_exists("curl_init"))
    die("cURL extension is not installed");

class Api
{
    public $request;
    private $api_url = API_URL;
    private $data;
    private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
    private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
    private $ssl = false; // If set false (it accpets any ssl) should false
    private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
    private $timeout = 60; // Timeout of retriving page in SECs when curl does next url
    
    public function setHeader($header)
    {
        $this->header = $header;
    }
    public function setFollow($follow)
    {
        $this->follow = $follow;
    }
    public function setSsl($ssl)
    {
        $this->ssl = $ssl;
    }
    public function setCtimeout($ctimeout)
    {
        $this->ctimeout = $ctimeout;
    }
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }
    public function __construct($request)
    {
        $this->request = $this->api_url . $request . '&api_key=' . api_key;
    }
    
    public function Connect()
    {
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, $this->request);
        curl_setopt($ch, CURLOPT_HEADER, $this->header);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        
        $this->data = curl_exec($ch);
        curl_close($ch);
        
    }
    
    public function Cached_Api_Results()
    {
        echo $cache_file = CACHE_FILE_DIRECTORY . $this->request . '.json';
        
        if (!$expires)
            $expires = time() - 2 * 30 * 30;
        
        if (!file_exists($cache_file)) {
            $handle = fopen($cache_file, 'w');
            fclose($handle);
        }
        
        // Check that the file is older than the expire time and that it's not empty
        if (filectime($cache_file) < $expires || file_get_contents($cache_file) == '') {
            // File is too old, refresh cache
            $api_results  = $this->Connect();
            $json_results = json_encode($api_results);
            
            // Remove cache file on error to avoid writing wrong xml
            if ($api_results && $json_results) {
                file_put_contents($cache_file, $json_results);
            } else {
                unlink($cache_file);
            }
        } else {
            // Fetch cache
            $json_results = file_get_contents($cache_file);
            $request_type = 'JSON';
        }
        
        return json_decode($json_results);
    }
    
    public function getResult()
    {
        return json_decode($this->data);
    }
    
}

$curlInstance = new Api("categories?");
//$curlInstance->Connect();
$curlInstance->Cached_Api_Results();

var_dump($curlInstance);

$results = $curlInstance->getResult();

echo '<ul>
    <li><a href="blog.php?c=">All Blog Posts</a></li>';

foreach ($results as $category) {
    echo '<li><a href="blog.php?c=' . $category->categorie_id . '">' . $category->categorie . '</a></li>';
}

echo '</ul>';

?> 

Link to comment
Share on other sites

Firstly I noticed you use function_exists() as opposed to extension_loaded(). I suppose neither are wrong in this instance however you may experience problems in the future if your implementing an entire extension but only checking if a specific function exists as one version could contain the function and another may not.

 

Secondly, your executing a $this->Connect() method which currently returns nothing therefore, $api_results will be null and the rest of your code likely break.

 

Come back once you've fixed those issues and taken a look at this post of yours for further corrections.

Link to comment
Share on other sites

Hi, I've changed the code to this again im getting results in $api_results however its not being written to file

 

<?php

/// Replace '*********' With your api key which can be obtained from http://www.socialnewsoffice.com
DEFINE('api_key', 'b5cbd66dca99c49d1a6c3d');

DEFINE('API_URL', 'http://www.socialnewsoffice.com/');

DEFINE('CACHE_FILE_DIRECTORY', $_SERVER['DOCUMENT_ROOT'] . 'cache/');

//check if you have curl loaded
if (!extension_loaded("curl")) {
    die( "cURL extension is not available<br />" );
}

class Api
{
    public $request;
    private $api_url = API_URL;
    private $data;
    private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
    private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
    private $ssl = false; // If set false (it accpets any ssl) should false
    private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
    private $timeout = 60; // Timeout of retriving page in SECs when curl does next url
    
    public function setHeader($header)
    {
        $this->header = $header;
    }
    public function setFollow($follow)
    {
        $this->follow = $follow;
    }
    public function setSsl($ssl)
    {
        $this->ssl = $ssl;
    }
    public function setCtimeout($ctimeout)
    {
        $this->ctimeout = $ctimeout;
    }
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }
    public function __construct($request)
    {
        $this->request = $this->api_url . $request . '&api_key=' . api_key;
    }
    
    public function Connect()
    {
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_URL, $this->request);
        curl_setopt($ch, CURLOPT_HEADER, $this->header);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        
        return curl_exec($ch);
        curl_close($ch);
        
    }
    
    public function Cached_Api_Results()
    {
        $cache_file = CACHE_FILE_DIRECTORY . $this->request . '.json';
        
        if (!$expires)
            $expires = time() + (60*30);
        
        if (!file_exists($cache_file)) {
            $handle = fopen($cache_file, 'w');
            fclose($handle);
        }
        
        // Check that the file is older than the expire time and that it's not empty
        if (filectime($cache_file) > $expires || file_get_contents($cache_file) == '') {
            // File is too old, refresh cache
            $api_results  = $this->Connect();
            $json_results = json_encode($api_results);
            
            // Remove cache file on error to avoid writing wrong xml
            if ($api_results && $json_results) {
                file_put_contents($cache_file, $json_results);
            } else {
                unlink($cache_file);
            }
        } else {
            // Fetch cache
            $json_results = file_get_contents($cache_file);
            $request_type = 'JSON';
        }
        
        $this->data = json_decode($json_results);
    }
    
    public function getResult()
    {
        return json_decode($this->data);
    }
    
}

$curlInstance = new Api("categories?");
//$curlInstance->Connect();
$curlInstance->Cached_Api_Results();

var_dump($curlInstance);

$results = $curlInstance->getResult();

echo '<ul>
    <li><a href="blog.php?c=">All Blog Posts</a></li>';

foreach ($results as $category) {
    echo '<li><a href="blog.php?c=' . $category->categorie_id . '">' . $category->categorie . '</a></li>';
}

echo '</ul>';

?> 

Link to comment
Share on other sites

On the topic of OO, you should be using Class Constants instead of Global Constants.  So:

 

class Api {

    const API_KEY = 'b5cbd66dca99c49d1a6c3d';
    const API_URL = 'http://www.socialnewsoffice.com/';
    private static $CACHE_FILE_DIRECTORY =  $_SERVER['DOCUMENT_ROOT'] . 'cache/';

    ....

    self::API_KEY;
    self::API_URL;
    self::$CACHE_FILE DIRECTORY;

Link to comment
Share on other sites

OK I've got the code to work should/ any improvements i should make?

 

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

/// Replace '*********' With your api key which can be obtained from http://www.socialnewsoffice.com
DEFINE('api_key', 'b5cbd66dca99c49d1a6c3d');

// Social News Office Api URL
DEFINE('API_URL', 'http://www.socialnewsoffice.com/');

// Place to store cache files
DEFINE('CACHE_FILE_DIRECTORY', $_SERVER['DOCUMENT_ROOT'] . 'cache/');

// 1 - 60 IN Minutes
DEFINE('CACHE_EXPIRES', '5');

//check if you have curl loaded
if (!extension_loaded("curl")) {
    die("cURL extension is not available<br />");
}

class Api
{
    private $request;
    private $api_url = API_URL;
    private $data;
    private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
    private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
    private $ssl = false; // If set false (it accpets any ssl) should false
    private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
    private $timeout = 60; // Timeout of retriving page in SECs when curl does next url
    
    public function setHeader($header)
    {
        $this->header = $header;
    }
    public function setFollow($follow)
    {
        $this->follow = $follow;
    }
    public function setSsl($ssl)
    {
        $this->ssl = $ssl;
    }
    public function setCtimeout($ctimeout)
    {
        $this->ctimeout = $ctimeout;
    }
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }
    public function __construct($request)
    {
        $this->request = $request;
    }
    
    public function Connect()
    {
        $ch = curl_init();
        
        $con = $this->api_url . $this->request . '&api_key=' . api_key;
        
        curl_setopt($ch, CURLOPT_URL, $con);
        curl_setopt($ch, CURLOPT_HEADER, $this->header);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        
        return curl_exec($ch);
        curl_close($ch);
        
    }
    
    public function Cached_Api_Results()
    {
        $cache_file = CACHE_FILE_DIRECTORY . $this->request . '.json';
        
        $expires = time() - (60 * CACHE_EXPIRES);
        
        if (!file_exists($cache_file)) {
            $handle = fopen($cache_file, 'w');
            fclose($handle);
        }
        
        echo filectime($cache_file) . '<' . $expires;
        
        // Check that the file is older than the expire time and that it's not empty
        if (filectime($cache_file) < $expires || file_get_contents($cache_file) == '') {
            // File is too old, refresh cache
            $api_results = $this->Connect();
            echo 'got new cache';
            $json_results = json_encode($api_results);
            
            // Remove cache file on error to avoid writing wrong xml
            if ($api_results && $json_results) {
                file_put_contents($cache_file, $json_results);
            } else {
                unlink($cache_file);
            }
        } else {
            // Fetch cache
            $json_results = file_get_contents($cache_file);
            $request_type = 'JSON';
        }
        
        $this->data = json_decode($json_results);
    }
    
    public function getResult()
    {
        return json_decode($this->data);
    }
    
}

$curlInstance = new Api("categories?");
//$curlInstance->Connect();
$curlInstance->Cached_Api_Results();

$results = $curlInstance->getResult();

if (isset($results) && $results != '') {
    echo '<ul>
    <li><a href="blog.php?c=">All Blog Posts</a></li>';
    
    foreach ($results as $category) {
        echo '<li><a href="blog.php?c=' . $category->categorie_id . '">' . $category->categorie . '</a></li>';
    }
    
    echo '</ul>';
    
}

?> 

Link to comment
Share on other sites

As for improvements, there are many.  The constants should be class constants.  Cache file location and expiration should be parameters passed to __construct()  and your method names should be rethought (e.g. "connect" is doing a lot more than establish a connection, and should be private).  Also, your naming convention is all wrong.  I suggest using camelCase for function names.  You're using Title_Case_With_Underscores, which I've only ever seen in class-naming convention (and even then, only rarely).

Link to comment
Share on other sites

@smoseley - A naming function naming convention cannot be wrong unless it doesn't work. Your - and mine funnily enough - preference is camelCase but other people may prefer to use underscores.

 

You cannot outright say "your naming convention is wrong" because if it were wrong, the code wouldn't work. E.g. starting the function name with an integer.

Link to comment
Share on other sites

@smoseley - A naming function naming convention cannot be wrong unless it doesn't work. Your - and mine funnily enough - preference is camelCase but other people may prefer to use underscores.

 

You cannot outright say "your naming convention is wrong" because if it were wrong, the code wouldn't work. E.g. starting the function name with an integer.

Fair enough.  Working with your own naming convention is all fine and good if you're working by yourself, but when you take a non-standard convention into a group, people will start refactoring your code for consistency, which is bad.

 

Best to train yourself to use the most widely accepted conventions, such that you'll work better in a team environment.

Link to comment
Share on other sites

I've done a few improvments hows it looking now?

 

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

if (!function_exists('curl_init')) {
    die("Social News Office needs the CURL PHP extension.");
}
if (!function_exists('json_decode')) {
    die("Social News Office needs the JSON PHP extension");
}

class SocialNewsOffice
{
    private $meta_title;
    private $meta_description;
    
    private $api_url = 'http://www.socialnewsoffice.com/'; // Social News Office API URL
    private $api_key;
    
    private $request;
    private $data;
    
    private $cache_expire = 5; // Cache Refresh in Minutes (1 - 60)
    private $cache_folder_location; // Where to store Cache Folder
    
    private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
    private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
    private $ssl = false; // If set false (it accpets any ssl) should false
    private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
    private $timeout = 60; // Timeout of retriving page in SECs when curl does next url
    
    function __construct($api_key, $cache_expire_in_minutes, $cache_folder_location)
    {
        $this->api_key               = $api_key;
        $this->cache_expire          = $cache_expire_in_minutes;
        $this->cache_folder_location = $cache_folder_location;
    }
    
    public function setHeader($header)
    {
        $this->header = $header;
    }
    public function setFollow($follow)
    {
        $this->follow = $follow;
    }
    public function setSsl($ssl)
    {
        $this->ssl = $ssl;
    }
    public function setCtimeout($ctimeout)
    {
        $this->ctimeout = $ctimeout;
    }
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }
    
    private function Connect()
    {
        $ch = curl_init();
        
        $con = $this->api_url . $this->request . '&api_key=' . $this->api_key;
        
        curl_setopt($ch, CURLOPT_URL, $con);
        curl_setopt($ch, CURLOPT_HEADER, $this->header);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        
        $response = curl_exec($ch);
        
        /* Check for 404 (file not found). */
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpCode == 404) {
            return '404';
        } else {
            return $response;
        }
        
        curl_close($ch);
    }
    
    public function CacheApiResult()
    {
        $cache_file = $this->cache_folder_location . $this->request . '.json';
        
        $expires = time() - (60 * $this->cache_expire);
        
        if (!file_exists($cache_file)) {
            $handle = fopen($cache_file, 'w');
            fclose($handle);
        }
        
        // Check that the file is older than the expire time and that it's not empty
        if (filectime($cache_file) < $expires || file_get_contents($cache_file) == '') {
            // File is too old, refresh cache
            $api_result = $this->Connect();
            
            try {
                if ($api_result == '404') {
                    throw new Exception("API: Page Not Found - 404");
                    // Remove cache file on error to avoid writing wrong json
                    unlink($cache_file);
                    
                } else {
                    file_put_contents($cache_file, json_encode($api_result));
                }
                
            }
            catch (Exception $e) {
                echo 'Error ' . $e->getMessage();
                
            }
            
        } else {
            // Fetch cache
            $this->data = json_decode(file_get_contents($cache_file));
        }
        
    }
    
    public function getCategories()
    {
        $this->request = 'categories?';
        $this->CacheApiResult();
        return json_decode($this->data);
    }
    
    public function getFeaturedNews()
    {
        $this->request = 'featured_news?';
        $this->CacheApiResult();
        return json_decode($this->data);
    }
    
    public function getBlogPosts()
    {
        $this->request = 'blog_posts?';
        $this->CacheApiResult();
        return json_decode($this->data);
    }
    
    public function getSettings()
    {
        $this->request = 'settings?';
        $this->CacheApiResult();
        return json_decode($this->data);
    }
    
}

?>

Link to comment
Share on other sites

Looking awesome, Danny!

 

A few final suggestions:

 

1) make API_URL a constant, so

 

const API_URL = 'http://www.socialnewsoffice.com/'; // Social News Office API URL

 

2) Connect() and CacheApiResults() should follow the same naming convention as the rest of your functions:

 

private function connect();
private function cacheApiResults();

 

3) __construct() as public in scope (there are cases where you'll have a private constructor):

 

public function __construct();

 

4) curl_init check inside the the SocialNewsOffice constructor, and use throw:

 

    public function __construct($api_key, $cache_expire_in_minutes, $cache_folder_location)
    {
        if (!function_exists('curl_init')) {
            throw new Exception("Social News Office needs the CURL PHP extension.");
        }
        if (!function_exists('json_decode')) {
            throw new Exception("Social News Office needs the JSON PHP extension");
        }

Link to comment
Share on other sites

How do i kill the application if they dont exists? Thanks for your help btw

 

    {
        if (!function_exists('curl_init')) {
            throw new Exception("Social News Office needs the CURL PHP extension.");
        }
        if (!function_exists('json_decode')) {
            throw new Exception("Social News Office needs the JSON PHP extension");
        }

Link to comment
Share on other sites

Nice thing about Exceptions - they give you a lot more options and information than die()

try {

    $sn = new SocialNewsOffice("abc", 5, "/home/steve/");

    // other code

} catch (Exception $e) {
    // Handle exception thrown by SNO (log it and redirect the user... good for a production environment):
    error_log("Something went wrong with SNO: " . $e->getMessage());
    header("location: /unhandled-exception.php");

    // or you can instead add a debug-mode handler to print the exception to the screen for debugging (good for a development environment):
    echo "Something went wrong! " . $e->getMessage();
    echo "<pre>";
    print_r($e->getTrace())
    echo "</pre>";
    exit();
}

Link to comment
Share on other sites

When i first start the code i get errors like this however when i refreash i dont i think it has something to do with reading from cache

 

Notice: Trying to get property of non-object in /home/sites/northplanet.co.uk/public_html/SocialNewsOffice.php on line 36

Notice: Trying to get property of non-object in /home/sites/northplanet.co.uk/public_html/SocialNewsOffice.php on line 37

Notice: Trying to get property of non-object in /home/sites/northplanet.co.uk/public_html/blog-test.php on line 32

Notice: Trying to get property of non-object in /home/sites/northplanet.co.uk/public_html/blog-test.php on line 33

Notice: Trying to get property of non-object in /home/sites/northplanet.co.uk/public_html/blog-test.php on line 34

Link to comment
Share on other sites

im using

<?php

if (!function_exists('curl_init')) {
    die("Social News Office needs the CURL PHP extension.");
}
if (!function_exists('json_decode')) {
    die("Social News Office needs the JSON PHP extension");
}

class SocialNewsOffice
{
    private $meta_title;
    private $meta_description;
    
    private $api_url = 'http://www.socialnewsoffice.com/'; // Social News Office API URL
    private $api_key;
    
    private $request;
    private $data;
    
    private $cache_expire = 5; // Cache Refresh in Minutes (1 - 60)
    private $cache_folder_location; // Where to store Cache Folder
    
    private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
    private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
    private $ssl = false; // If set false (it accpets any ssl) should false
    private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
    private $timeout = 60; // Timeout of retriving page in SECs when curl does next url
    
    public function __construct($api_key, $cache_expire_in_minutes, $cache_folder_location)
    {
        $this->api_key               = $api_key;
        $this->cache_expire          = $cache_expire_in_minutes;
        $this->cache_folder_location = $cache_folder_location;
	$settings = $this->getSettings();
	$this->meta_title = $settings['0']->meta_title;
	$this->meta_description = $settings['0']->meta_description;
    }
    
    public function setHeader($header)
    {
        $this->header = $header;
    }
    public function setFollow($follow)
    {
        $this->follow = $follow;
    }
    public function setSsl($ssl)
    {
        $this->ssl = $ssl;
    }
    public function setCtimeout($ctimeout)
    {
        $this->ctimeout = $ctimeout;
    }
    public function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }
    
    private function connect()
    {
        $ch = curl_init();
        
        $con = $this->api_url . $this->request . '&api_key=' . $this->api_key;
        
        curl_setopt($ch, CURLOPT_URL, $con);
        curl_setopt($ch, CURLOPT_HEADER, $this->header);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        
        $response = curl_exec($ch);
        
        /* Check for 404 (file not found). */
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpCode == 404) {
            return '404';
        } else {
            return $response;
        }
        
        curl_close($ch);
    }
    
    public function cacheApiResult()
    {
        $cache_file = $this->cache_folder_location . $this->request . '.json';
        
        $expires = time() - (60 * $this->cache_expire);
        
        if (!file_exists($cache_file)) {
            $handle = fopen($cache_file, 'w');
            fclose($handle);
        }
        
        // Check that the file is older than the expire time and that it's not empty
        if (filectime($cache_file) < $expires || file_get_contents($cache_file) == '') {
            // File is too old, refresh cache
            $api_result = $this->connect();
            
            try {
                if ($api_result == '404') {
                    throw new Exception("API: Page Not Found - 404");
                    // Remove cache file on error to avoid writing wrong json
                    unlink($cache_file);
                    
                } else {
                    file_put_contents($cache_file, json_encode($api_result));
                }
                
            }
            catch (Exception $e) {
                echo 'Error ' . $e->getMessage();
                
            }
            
        } else {
            // Fetch cache
            $this->data = json_decode(file_get_contents($cache_file));
        }
        
    }

 public function getMetaTitle()
    {
        return $this->meta_title;
    }

public function getMetaDescription()
    {
        return $this->meta_description;
    }
    
    public function getCategories()
    {
        $this->request = "categories?";
        $this->cacheApiResult();
        return json_decode($this->data);
    }
    
    public function getFeaturedNews()
    {
        $this->request = "featured_news?";
        $this->cacheApiResult();
        return json_decode($this->data);
    }
    
    public function getBlogPosts($start=0, $display=6, $categorie=NULL)
    {
	if($start == ''){ $start = 0; }
	if($display == ''){ $display = 6; }

        $this->request = "blog_posts?start=$start&display=$display&categorie=$categorie";
        $this->cacheApiResult();
        return json_decode($this->data);
    }
    
    public function getSettings()
    {
        $this->request = 'settings?';
        $this->cacheApiResult();
        return json_decode($this->data);
    }
    
}

?>

 

and

 

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

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

/// Replace '*********' With your website address
DEFINE ('BLOG_BASE_URL', 'http://www.northplanet.co.uk/');

$sno = new SocialNewsOffice("b5cbd66dca99c49d1a6c3d", '5', $_SERVER['DOCUMENT_ROOT'].'cache/');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $sno->getMetaTitle(); ?></title>
<meta name="Description" content="<?php echo $sno->getMetaDescription(); ?>" />
</head>

<body>

<?php


$blog_posts = $sno->getBlogPosts('0');

if (isset($blog_posts[0]->error[0]->code) && $blog_posts[0]->error[0]->code == '458') {
        echo '<h3>Not Found <span class="arrows">»</span></h3>
   <p>Sorry, but you are looking for something that is not here.</p>';
        
    } else {
        $start   = $blog_posts[0]->start;
        $pages   = $blog_posts[0]->pages;
        $display = $blog_posts[0]->display;
        
        echo '<h3>Recent Blog Posts<span class="arrows">»</span></h3>';
        
        foreach ($blog_posts[0]->data as $post) {
            echo '<div class="block-item-big">
              <h2><a title="' . strip_tags($post->post_title) . '" href="' . BLOG_BASE_URL . 'article/' . $post->post_url . '/">' . strip_tags($post->post_title) . '</a></h2>
              <div class="block-image"><a title="' . strip_tags($post->post_title) . '" href="' . BLOG_BASE_URL . 'article/' . $post->post_url . '/">
              <img width="250" height="190" alt="' . strip_tags($post->post_title) . '" src="' . BLOG_BASE_URL . 'sno-blog/src.php?src=' . $post->blog_img . '&w=250&h=190&mode=fit"></a></div>
              <span class="block-meta"><span class="heading-date">' . $post->post_date . '</span>
              <p>' . substr(strip_tags($post->post_content, '<p><br><a>'), 0, 298) . '...' . '</p>
              <a class="readmore" title="' . strip_tags($post->post_title) . '" href="' . BLOG_BASE_URL . 'article/' . $post->post_url . '/">Read More <span class="block-arrows">»</span></a>
              </div>';
            
        }
}



?>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.