Osmek.php Documentation

Osmek.php is developed and maintained by the Osmek team. It is the recommended library for PHP implimentations.

Download Osmek.php

Configuration

To start, pass your API key to the class when you instantiate it.

<?php

require_once('Osmek.php');
$osmek = new Osmek('myapikey1234567890');

You can also configure the class by passing an array instead of your API key. (Just make sure to include your API key in the array).

<?php

$config 
= array(
    
'account_api_key' => 'myapikey1234567890'
    
'cache_feeds' => true
    
'cache_folder' => './osmek_cache/' 
);

$osmek = new Osmek($config);

Reference

Use the following methods to build your site.

load_account()

Checks Osmek connection and loads your account info. Once this method is called, you can use a section slug in place of a section ID in your subsequent calls.

Returns boolean false if there was a connection or authentication error.

<?php

if( ! $this->osmek->load_account())
{
    die(
'Osmek error: '.$this->osmek->msg);
}

fetch_feed()

Fetches a feed.

fetch_feed(array $data [, $force_cache = 'default'])

$data should be an array of data to post to the api. If your api key is not included in this array, it will be set to the api key passed in when the class was instantiated. $force_cache allows you to override the cache settings for a single call.

<?php

$feed 
$osmek->fetch_feed(array('section_id' => 123'format' => 'html''limit' => '1'));

Format Specific Methods

The following methods simplify the fetch_feed() method for specific response formats.

fetch_template()

Fetches a feed from Osmek formatted as a template. Learn more about Osmek templates here.

fetch_template(int $section_id, string $template [, array $options [, boolean $forcecache]])
<?php

$template 
'
    <h1>[title]</h1>
    <span class="date">[date *m/*d/*Y]</span>
    [description page:1]
'
;
$options = array('limit' => 1);
echo 
$osmek->fetch_template(101$templateoptions);

fetch_json()

Fetches a feed from Osmek formatted as json.

fetch_json(int $section_id [, array $options [, boolean $forcecache]])
<?php

echo $osmek->fetch_json(101);

fetch_html()

Fetches a feed from Osmek formatted as html.

fetch_html(int $section_id [, array $options [, boolean $forcecache]])
<?php

echo $osmek->fetch_html(101);

fetch_xml()

Fetches a feed from Osmek formatted as xml.

fetch_xml(int $section_id [, array $options [, boolean $forcecache]])
<?php 

echo $osmek->fetch_xml(101);

fetch_vars()

Returns an array of objects. (Set argument 5 to true to return associative arrays instead).

fetch_vars(int $section_id, array $vars [, array $options [, boolean $force_cache [, boolean $associative_array]]])
<?php

$vars 
= array('title''date *m/*d/*Y''description');
$data $osmek->fetch_vars(101$vars);

// $data will look something like this
/*
Array
(
    [0] => object(stdClass) (3) {
        ["title"]=> string(11) "Lorem Ipsum"
        ["date"]=> string(10) "12/22/2009"
        ["description"]=> string(21) "<p>Dolor Sit amet</p>"
    },
    [1] => object(stdClass) (3) {
        ["title"]=> string(21) "Vestibulum vitae ante"
        ["date"]=> string(10) "12/29/2009"
        ["description"]=> string(41) "<p>non augue posuere aliquet sed quis</p>"
    }
)
*/

If you want to use a variable more than once, you can give it a named key

<?php

$vars 
= array('title''month' => 'date *F''year' => 'date *Y''description');
$data $osmek->fetch_vars(101$vars);


// $data will look something like this
/*
Array
(
    [0] => object(stdClass) (4) {
        ["title"]=> string(11) "Lorem Ipsum"
        ["month"]=> string(8) "December"
        ["year"]=> string(4) "2009"
        ["description"]=> string(21) "<p>Dolor Sit amet</p>"
    },
    [1] => object(stdClass) (4) {
        ["title"]=> string(21) "Vestibulum vitae ante"
        ["month"]=> string(8) "December"
        ["year"]=> string(4) "2009"
        ["description"]=> string(41) "<p>non augue posuere aliquet sed quis</p>"
    }
)
*/

fetch_comments()

Fetch comments for an item.

fetch_comments(int $section_id, int $item_id [, array $options [, $force_cache]])

fetch_comment_vars()

Fetch comments for an item. Works the same as fetch_vars().

fetch_comments(int $section_id, int $item_id, array $vars [, array $options [, $force_cache]])

account_info()

Returns account information as JSON.

account_info(int $section_id [, boolean $force_cache])

section_info()

Returns section information as JSON.

section_info(int $section_id [, array $data [, boolean $force_cache]])

Create/Update/Insert Methods

create()

Creates an item in the given section.

create(int $section_id, array $data)

update()

Updates an item in the given section.

update(int $section_id, int $item_id, array $data)

new_contact()

Creates a new contact.

new_contact(int $section_id, array $data)

make_comment()

Makes a comment on an item.

make_comment(int $section_id, int $item_id, array $comment [, array $options])

subscribe()

Subscribes an email address to a section

subscribe(string $email, int $section_id)

check_login()

Check an Osmek username and password. If you've called load_account() already, the account ID will be stored in $osmek->account_id

If you specify a return_token in the data array, Osmek will start a session for the user, and return a token allowing you to forward the user to Osmek, already logged in.

check_login(string $account_id, string $username, string $password, array $data)
<?php

$user_email 
$_POST['email'];
$user_pass $_POST['pass'];

/* Load the account info so we have access to the account ID */
$osmek->load_account();

/* Check username and pass, and generate a session token */
$rsp json_decode($osmek->check_login($osmek->account_id$user_email$user_pass, array('return_token' => true)));

if(
$rsp->status == 'ok')
{
    
// Login successful!
    
    // We can now forward our user on to Osmek with the token provided
    
if(isset($rsp->token))
    {
        
// Create an MD5 hash of our account id + the user's email addres, + the token
        
$hash md5($osmek->account_id $user_email $rsp->token);
        
header('Location: http://cms.osmek.com/login/token/'.$hash);
    }
    
}

upload_photo()

Upload a photo to your account

upload_photo(string $file_name, string $tmp_name [, array $options])

clear_cache()

If you have caching turned on, calling this function will delete all of the cached files.

clear_section_cache()

Clear cache files for a specific section. Useful in combination with the postback url setting in Osmek.

clear_section_cache(mixed $section_id)
<?php

$updates 
file_get_contents("php://input");
$data json_decode($updates);
        
/* Clear cache for this section */
if(isset($data->section_id))
{
    
$osmek->clear_section_cache($data->section_id);
}