# Invoking and accessing the API

There are 3 ways of invoking and accessing the Open-Realty API:

1. Invoke internally from local add-ons, or hooks.

Using the API from within an  add-on is very simple.

  • Declare the global variable $api in your function
  • Call $api->load_local_api('API_FUNCTION',$params);

*where API_FUNCTION is a valid API function

Example: Add a remotely linked photo to a specific listing, using the 'media__create' API function:

function myaddon_addPhotoFromURL(){

global $api;

//Create the Media Object(photo) we want to add

$mediaobject['some_photo.jpg']['data']='http://www.somesite.com/some_photo.jpg';

//Tell the API that this media object should be linked, and not stored locally.

$mediaobject['some_photo.jpg']['remote']=TRUE;

//Link this remote photo to Listing ID# 112

$result=$api->load_local_api(

'media__create', array(

'media_parent_id'=>'112',

'media_type'=>'listingsimages',

'media_data'=>$mediaobject

)

);

echo '<pre>'.print_r($result,TRUE)."</pre>\r\n";

}

?>

2. Invoke internally from other PHP scripts within the same hosting account as your OR software.

This is also simple to do and works like the Internal API call in the example above.

  • Include the api.inc.php file and load the api class.
  • Call $api->load_local_api('API_FUNCTION',$params);

Example: adding a linked photo to a specific listing

<?php

require('/path/to/open-realty/api/api.inc.php');

$api = new api();

//Define the Media Object(photo) we want to add

$mediaobject['some_photo.jpg']['data']='http://www.somesite.com/some_photo.jpg'];

//Instruct the API that this media object should be linked, and not stored locally.

$mediaobject['some_photo.jpg']['remote']= TRUE;

//Add this photo to Listing 112 using the 'media__create' API function

$result = $api->load_local_api('media__create', array(

'media_parent_id'=>'112',

'media_type'=>'listingsimages',

'media_data'=>$mediaobject

));

echo '<pre>'.print_r($result,TRUE)."</pre>\r\n";

?>

3. Invoke externally from remote websites or apps using remote API calls to connect to a host OR web site.

Invoking and using the API remotely is also very straightforward. The remote website or app does not have to be using OR itself, but must have PHP available and be able to make outbound HTTP/HTTPS connections to the host OR site.

  • Copy the /api/api.inc.php file from the host OR site to a location on the remote web site where you want to run the remote api commands from.
  • Create a file in the same location as the api.inc.php file named 'common.php' as follows. You then need to place the API Key from the remote OR site you want to connect to (obtain from its Site Config) within this file and assign it to the "apikey" $config array.

custom common.php file:

<?php

global $config;

$config['apikey'] = 'THE OR HOST SITE API KEY GOES HERE';

?>

  • Setup/create your remote API commands in a different file such as example.php or within your custom php script, the following is an example for creating a new property class using a remote API call..

example.php. This example creates a new "Residential Property" property class on the distant host OR site and echos its new class ID# if successful.

<?php

//include the API file, assumes it is in the same folder as this

require dirname(__FILE__).'/api.inc.php';

$orapi = new api();

//Define API Server. This is the host OR site you wish to connect to

define('API_SERVER','http://yourwebsite.com/admin/index.php');

//Set login credentials:

//This does not have to use the Admin account,

//but specific account permissions are enforced.

define('API_USER','admin');

define('API_PASS','password');

//Create a new property class named Residential Property

//using the 'pclass__create' API function

$result = $orapi->send_api_command(

API_SERVER,

'pclass__create', array(

'class_system_name'=>'Residential Property',

'class_rank'=>1

),

API_USER,

API_PASS

);

if($result['api_result_code'] != FALSE){

echo '<pre>Failed Creating Class: '.print_r($result,TRUE).'</pre>';

die;

}

else{

$class_id =$result['api_result']['class_id'];

}

echo $class_id;

?>

The examples for the methods in the following sections assume you are invoking the API locally as in #1 above.