Sie sind auf Seite 1von 10

Importing your PHP Application

to the Etelos™ Platform


Get the most out of your PHP applications
in the Etelos™ Environment

1 of 10
Import your PHP applications to the Etelos™ platform
Etelos™ lets you work in the development environment of your choice. Building apps with Etelos™ is
easy – write the application it in your development environment, move it to the Etelos™ framework, and
it’s done. Once there, it can integrate with other applications, enable AppSync™ rules, work offline using
Apps On a Plane™, and share information with other Etelos™ applications. It’s up to you.

This tutorial gives instructions on how to import your PHP application to the Etelos™ environment.

Modify Your PHP Application


In order to make your PHP application work on the Etelos™ framework, you need to tweak the source
code.

Etelos™ supports the MySQL and PostgreSQL database systems. However, in order to access your
app’s database in the Etelos framework, you need to add code to your application that helps establish
database credentials.

By following these steps, you’ll get your database-driven PHP applications up and running on the Etelos
Platform.

Prerequisites
You need to make sure you have the following conditions met with your PHP application
before using it in the Etelos™ platform.

 A working PHP application with a linked database

 Be able to directly edit the PHP source code

 An Etelos™ Developer Kit and account. Visit www.etelos.com/developer to sign


up for the free Etelos™ Developer Kit.

 An active Etelos™ Marketplace account. Visit www.etelos.com/marketplace to


sign up for an Etelos™ Marketplace account hosted on the Etelos™ network.

 Your existing MySQL or PostgresQL database, migrated to your Etelos


application’s database. (Use MySQL or PostgreSQL’s dump utility functions to
create a new database on your Etelos Developer account server.)

 Your preferred PHP authoring tools, and any other PHP utilities that you use to
build your PHP applications. Or, log into the Etelos™ Application Management
Interface in your Etelos™ Developer account.

Importing PHP to the 2 of 10


Etelos Developer Environment
Step 1: Get the Etelos™ PHP code samples and Helper
Make sure you have all of the Etelos helper files on your computer, ready to integrate into your
PHP code.

Step 2: Upload Your PHP to the Etelos Framework


Upload your PHP code to your hosted Etelos account from your local computer. Make sure that
you have a new copy of your MySQL or PostgreSQL database migrated to your Etelos Developer
Account using the mysqldump or pg_dump utility functions in MySQL or PostgreSQL.

[SECTION ON HOW TO USE THE WEB INTERFACE TO PORT DUMPFILE]

Once you’ve got a dump file of your MySQL database or your PostgreSQL database, you need to
create the database in your Etelos server account.

NOTE: If you do not have the ability to perform a dump of your MySQL or PostgreSQL database
to the Etelos platform, you will have problems packaging your source code for release
and sale on the Etelos Marketplace.

Step 3: Include the Helper File in your PHP code


The etelos_ini_helper.php class makes it simple to retrieve database credentials from the Etelos
configuration files.

Download the Etelos™ PHP Helper File (etelos_ini_reader.php) to your development environment,
and put the Etelos™ PHP Helper File into your Etelos account. (For a quick video demonstration of
how to use WebDAV to log into your Etelos account server, visit
http://www.etelos.com/videos/training/webdav/webdav.html). If you need a copy of the Etelos™ PHP
Helper File, download it from the Etelos Marketplace.

To test the etelos_ini_reader.php code, locate the ini_example file. Place the ini_example file in
the same directory as your etelos_ini_reader.php file.

Use your web browser to load the ini_example file.

Your web page’s output should look something like:

Importing PHP to the 3 of 10


Etelos Developer Environment
If it doesn’t, there was an error. The error page should look like:

You should be able to see the error in the body of the page.

If you show an error, reload the file to your WebDAV environment, or refresh the page.

If you continue to see the error, contact Etelos Developer support at:
V6.Developer.Support@etelos-inc.com.

Importing PHP to the 4 of 10


Etelos Developer Environment
Step 3: Integrate the Helper File with your PHP application.
Identify the places in your program where you need to find or specify your database credentials. These
are the places you will need to use the PHP integration code.

In all of the applicable database credential areas, add the following code sample to your application.
Make sure you add it to all the applicable database credential areas. It’s permittable to remove the
commented sections prior to implementation.

<?php
// These variables will store the database data or the error message.
$data = false;
$errorMessage = false;
// Include the helper class
require_once("etelos_ini_reader.php");
// Try to read the data
try{
// Try to load the INI data
$reader = new etelos_ini_reader();
$data = $reader->getData();
$errorMessage = false;
// If no exception was thrown by this point, the helper file worked.
}catch(Exception $e){
// An exception was thrown, there was a problem reading the INI file.
$errorMessage = $e->getMessage();
$data = false;
}
/**
* At this point we either have the INI data, or an error. How you deal with the
* error is up to you. The $data array should be associative with the
* following keys:
*
* dbtype Generally either "postgresql" or "mysql"
* dbhost Hostname of database server
* dbname Name of the database
* dbport TCP/IP port number, may be blank for default or N/A
* dbuser Username
* dbpass Password
*
*/
?>

Once you’ve added this code to all of the database credential areas, save and upload your PHP

Importing PHP to the 5 of 10


Etelos Developer Environment
application.

Now, make sure that your PHP application works with the Etelos™ framework.

Importing PHP to the 6 of 10


Etelos Developer Environment
Step 4: Testing and Troubleshooting
Once you’ve implemented the helper files and included the database credential code, you should be able
to call, retrieve, modify, and change information in your PHP database.

Load your application, and make sure that the code stores and retrieves information from your database
as intended. Check to make sure that your program can connect to the database from the Etelos™
framework, and that your database has information available to call.

Common troubleshooting problems:

 Make sure that your database has information in it (database entries). There will be an error code
returned if the PHP code calls an empty database. If you do not have database entries, perform a
dump of your database information from either PostgreSQL or MySQL using the included dump
utilities.

 Make sure you are calling an Etelos-supported database (MySQL or PostgreSQL).

 Make sure that you have the etelos_helper_ini.php file installed and enabled in the correct
directory with your PHP files.

 Make sure that the called database is functional and has uncorrupted information.

 If you run into any trouble importing your PHP files into the Etelos™ Development Environment,
check out the Etelos™ Developer resources at www.etelos.com/developer for articles, videos,
how-tos, discussion forums, and more. Or email the Etelos™ Developer Support Team at
V6.Developer.Support@etelos-inc.com.

That’s it! You’re done!


Congratulations! Your PHP application should be fully functional now on the Etelos™ Platform. Now you
can package your application, sell it on the marketplace, integrate Etelos™ Apps on a Plane™ and
AppSync™ functionality, and sell it to the Etelos™ community.

Importing PHP to the 7 of 10


Etelos Developer Environment
Appendix A: etelos_ini_helper.php Code Sample

The following code sample enables your PHP application in the Etelos™ environment. If
needed, you may create a new PHP file with etelos_ini_helper.php from the following
code sample.

<?php
/**
* This class is meant to help developers make their application portable on the Etelos™ platform. It makes it easy to access data
from the account_info.ini file.up
* Copyright 2008 Etelos™ Inc. Reuse and modification is granted only for the purposes of developing applications on the Etelos™
platform.
*/
class etelos_ini_reader{
const FILENAME = "account_info.ini";
protected $path;
protected $ini_data;
protected $section;
/**
* Creates a new reader.
*
* @throws Exception if unable to find or parse the INI file
*/
public function __construct(){
if(!isset($_SERVER['SERVER_NAME'])){
throw(new Exception("Error: Server name not set, cannot load per-app data"));
}
$this->path = $this->findFile();
if(!$this->path){
throw(new Exception("Error: Unable to find file: ".self::FILENAME));
}
$this->ini_data = $this->loadData();
if($this->ini_data == false){
throw(new Exception("Error: Unable to read or parse: ".$this->path));
}
if(function_exists("mb_strtolower")){
$this->section = "vhost:".mb_strtolower($_SERVER['SERVER_NAME']);
}else{
$this->section = "vhost:".strtolower($_SERVER['SERVER_NAME']);
}
if(!array_key_exists($this->section,$this->ini_data)){
throw(new Exception("Error: The INI file has no entry for the current vhost
(".$_SERVER['SERVER_NAME'].")") );
}

Importing PHP to the 8 of 10


Etelos Developer Environment
}
/**
* Finds the INI file based on a few locations
* 1) Adjacent to this file
* 2) Document-root
* 3) Document-root's parent
* 4) Current working directory, if from command-line
*
* @return string Path to file
*/
protected function findFile(){
$path = false;
$ini_search_paths = array();
$ini_search_paths[] = dirname(__FILE__);
if(array_key_exists('DOCUMENT_ROOT',$_SERVER)){
$ini_search_paths[] = $_SERVER['DOCUMENT_ROOT'] ;
$ini_search_paths[] = dirname($_SERVER['DOCUMENT_ROOT']); //One dir down
}
if(array_key_exists('PWD',$_ENV)){
// Being launched from command line, include current working dir
$ini_search_paths[] = $_ENV['PWD'];
}
foreach($ini_search_paths as $testpath){
$temp = $testpath.'/'.self::FILENAME;
if(is_file($temp)){
$path = $temp;
break;
}
}
return($path);
}
/**
* Loads data from the INI file. Returns false on error.
*
* @return An associative array of loaded values or false on error.
*/
protected function loadData(){
if(!is_readable($this->path)){
return(false);
}
// Load file
$data = parse_ini_file($this->path,TRUE);
return($data);

Importing PHP to the 9 of 10


Etelos Developer Environment
}
/**
* Returns an associative array of database credentials
*
* Keys are: [dbtype, dbhost, dbname, dbuser, dbpass, dbport]
* dbtype is generally either postgresql or mysql
*
* @param boolean $writable If true, then ensure credentials refer to the master server if there is a master-slave replications set-
up.
* @return array Associative array of credentials
*/
public function getData($writable = false){
$keys = array('dbtype','dbhost', 'dbname', 'dbuser', 'dbpass', 'dbport');
if($writable){
$key_prefix = 'write';
}else{
$key_prefix = 'read';
}
$ret = array();
$source = $this->ini_data[$this->section];
foreach($keys as $key){
$src_key = $key_prefix."_".$key;
if(array_key_exists($src_key,$source)){
$ret[$key] = $source[$src_key];
}else{
$ret[$key] = "";
}
}
return($ret);
}
}
?>

Importing PHP to the 10 of 10


Etelos Developer Environment

Das könnte Ihnen auch gefallen