Saturday, May 7, 2011

Cakephp-facebook-integration

* Version 1.5  
* Author : Chilarai Mushahary  
* Email : chilly5476@gmail.com  
* Language: Php  
* License: GNU/GPL


LATEST: Full cakephp_1_3_8 -facebook -integration application (beta) released based on the cakephp-facebook-component(version 1.5) with auto installer. check out at sourceforge or github


Download
Sourceforge  
Github 

Screenshots








CHANGELOG 1.3  
* 1. Added 'app.tar.gz'.  
* 2. Improvement in application access  
* 3. Added 'canvas page url' in config.php to directly access the application. * 4. Automatic display of relevant pages (login, permissions page, etc) in any actions that you call  
* 5. Removed loopback in default.ctp  
* 6. Complete 'app folder' with tests_controller and view files.  


CHANGELOG 1.4  
* 1. Added sample tests_controller with the new component  
* 2. Help file update  


CHANGELOG 1.5  
* 1. Added friend request, app request, gift request  
* 2. Operate on the request ids from app requests and delete the request from facebook once clicked on the link  
* 3. Get the 'facebook_access_token' by calling the function directly from the component wherever in the controller  
* 4. Get your friends' profile picture from your controller  
* 5 Auto logout from app when logged out of facebook  
* 6. All codes modified in app.tar.gz. Latest code updated for the new component  
* 7. Updated Help file  


NEXT CHANGELOG  
* 1. Embed youtube, mp3 in the facebook wall posts with the new facebook-api * * 2. Full pledged support for facebook-javascript sdk  
* 3. Will be based on the same component. So dont have to worry about the existing codes  


# NOTE :  
IN CASE YOU WANT TO COPY THE 'app' FOLDER, EXTRACT THE app.tar.gz AND REPLACE YOUR app FOLDER IN CAKEPHP. ELSE IN CASE YOU FOLLOW THE STEPS BELOW, COPY THE 'facebook.php' COMPONENT FROM APP.TAR.GZ FILE. IT HAS SOME UPDATED CODE  


CONFIGURATION  
* 1.Uncompress and copy the whole "facebook" folder in your `app/vendors/` 

* 2.Copy the following in the bottom most section of your `app/config/core.php` file: (you can go to "http://facebook.com/developers/apps.php" to see your application. To create new app goto http://facebok.com/developers/createapp.php)


Configure::write('AppId','YOUR_APP_ID'); //--> from facebook app page Configure::write('AppKey','YOUR_API_KEY'); //--> from your facebook app page Configure::write('AppSecret','YOUR_APP_SECRET'); //--> from your facebook app page Configure::write('AppUri','APP_ADDRESS'); //--> your app folder (eg localhost/cake) Configure::write('CanvasPage','CANVAS_PAGE_URL'); //--> your canvas page (eg http://apps.facebook.com/APP_NAME)  


All these parameters are obtained from facebook. visit http://facebook.com/developers/apps.php to see your apps  


* 3.Copy `facebook.php` file in your `app/controllers/components/` folder  


* 4.Go to your application page in facebook and click on `edit_settings`. Go to `website` link and then do as follows:
Type `http://server_address/path_to_your_app/` (remember to put the backslash in the end) in the first text field (eg. http://localhost/app_path) 
Type `server_address` in the second text field (eg, localhost) 


* 5. Copy `default.ctp` file in your `app/views/layouts/` folder  



BUGFIXES  
*1. I have found many people complaining in the forums about the app not loggin out by itself when loogin out of the facebook. I have modified the component to kill the existing cookies and the sessions in the component once you logout from facebook. You need not do anything in the code itself. The component itself will handle it.  


*2. The existing data about facebook maintaining its own session and not allowing to create our own sessions is invalid. Facebook do allows you to maintain sessions but any major change that you do in your code with cakephp takes some time for facebook to ammend in its canvas. Hence, if you have developed any code and you feel it is not behaving properly but the code is absolutely right, then wait for sometime and maybe try to call the url from facebook directly for few times and then it will work alright  
------------------------------------------------------------------  



NB: IMPORTANT  
#### To send Facebook game requests instead of app requests you do not need to change in the code. You have to modify the settings in the application. To do that you need to: 1. Go to http://facebook.com/developers/apps.php 2. Go to application profile page 3. Edit app 4. Set application as game or any other app according to the app content  


#### Deleting app request once you click on the Accept button Facebook by default does not delete your app request which your app sends, you need to do it through your app itself. ie, if some user x in Mafia Wars sends you a healthdrink and you click on the accept button, the request doesnot go away by itself. Mafia wars delete the request and not facebook.  


#### Remember, when you click on the accept button, you get both post and get requests The get request will get you the request id of the request The post request will contain all the data. ie request_id, sender_id, receiver_id, message, etc  


EXAMPLES  
* In any controller, import the facebook component as:
var $components = array('Facebook');  
* Then in any of the functions/actions you can type the following:

* To get the access token
debug($this->Facebook->getAccessToken());

* To get your "friend list", type debug(json_decode(file_get_contents($this->Facebook->getFriends()),true);

* To get your "own details", type debug(json_decode(file_get_contents($this->Facebook->getMyDetails())),true);

* To get your "profile picture", type
debug($this->Facebook->getProfilePicture($facebook_id=null)); where
$facebook_id = facebook id of the user
if $facebook_id == null, you will get your own profile picture

* To see if you have any request for this "application pending", type debug($this->Facebook->getAppRequests());

* To see all your "albums", type debug(json_decode(file_get_contents($this->Facebook->getAlbums())),true);

*To post on your "wall"
debug($this->Facebook->postWall($mainHeader, $subHeader=null, $appLink, $appName, $description, $pictureLink=null));
where,
$subHeader and $pictureLink are optional

* To "send app requests"
debug($this->Facebook->appRequest($message, $page=null, $data=null)); where
$message = message to appear in the send app request box $page = page to redirect once the send request is done (if null redirect to the app uri) $data = additional data to be passed from the sender (optional)


We can do a lot more, but till now, I have written only these. You can access your pictures, comments, wall, about_me, etc and a lot more. In future, the intention of this program is to write a full pledged plugin, so that a developer is able to import the plugin to readily integrate with cakephp and facebook.
Enjoy doing

Wednesday, September 1, 2010

Recess php framework and Facebook Integration - Part 1

Today, I will describe the features of Recess-PHP-Framework. This is another PHP based lightweight opensource framework developed by Kris Jordan. A relatively new framework compared to zend, cake, etc, but as with any other good frameworks, its feature rich, object oriented, well maintained and developed. It has support for databases like MySQL, SQLite

You may check out the Recess website here.
 Download the latest stable release of Recess here: Tar , Zip or from Github site


INSTALLATION

On Windows (with wamp):

Requirements

> wamp(includes apache, php, mysql): php5.2.4 or higher is recommended.
1) Install wamp by double clicking it and follow the instructions on the screen. In case, you are unsure of any parameter during installtion, leave the default parameters. Once installed, start wamp.

2) The default folder for wamp installation is c://wamp. Unzip/Untar the downloaded recess source and copy the whole folder to c://wamp/www/recess_folder_name

3) Browse to c://wamp/recess_folder_name/ and open the file named recess-conf.php. Search for "RecessConf::$namedDatabases" and "RecessConf::$defaultDatabase". Uncomment the lines with "mysql" parameters. Fill in your server details.

e.g In "RecessConf::$defaultDatabase", uncomment the line //'mysql:host=localhost;dbname=DBNAME', 'USER', 'PASS' and modify as 'mysql:host=localhost;dbname=name', 'root', ''.
In wamp, default USER is root and PASS is blank (hence ''). Open a browser and type in http://localhost/phpmyadmin. Create a new database named 'name'

4) Open a browser and type in "http://localhost/path_to_recess". if you see a screen like the one shown below,you are ready to use recess



INSTALLATION

On Linux (with lamp): Ubuntu

1) Install php apache and mysql: Open a shell and type in
sudo apt-get install php5 apache2  mysql-server

2) Download phpmyadmin, and move the untared file to /var/www/phpmyadmin

3) Copy all the untared recess files in /var/www/recess_folder_name

4) Start apache : Open a console and type
sudo /etc/init.d/apache2 start

5) Open the recess-conf.php file
sudo gedit /var/www/recess_folder_name/recess.conf.php

Search for "RecessConf::$namedDatabases" and "RecessConf::$defaultDatabase". Uncomment the lines with "mysql" parameters. Fill in your server details.

e.g In "RecessConf::$defaultDatabase", uncomment the line //'mysql:host=localhost;dbname=DBNAME', 'USER', 'PASS' and modify as 'mysql:host=localhost;dbname=name', 'root', '123'.Enter USER is root and PASS as you entered for mysql. Open a browser and type in http://localhost/phpmyadmin. Create a new database named 'name'

6) Open a browser and type in "http://localhost/path_to_recess".If you see an image likeabove, you are ready to start recess



To be continued...

Monday, July 27, 2009

PHP - MySQL maximum queries per second test results

I am writing this post here to share my findings on the maximum query execution speed of MySQL5 with php5 on two different systems. Both the tests were conducted on the local server.

The system configurations of the test PCs were as:
1) Avinash OS : Windows XP SP2
Intel P4, 3.066 GHz, 1.25 GB RAM

2) Ajay OS : Windows Vista SP2
AMD 64x2, 1.9 GHz, 3.00 GB RAM

Following were the configurations of WAMP server
Apache 2.2.11
PHP 5.2.8
MySQL 5.1.30

Below are the figures of my memory usage on the two computers before the test






Fig1. Avinash Memory Usage











Fig 2. Ajay Memory Usage








I have written the MySQL queries with PHP and obtained the completion time to perform the set of queries in Firefox 3.5.1. I chose Mozilla because currently its the most popular browser used by people today and no other reasons. Also, I have used MYSQLI extension which is a bit slower (negligible to a large extent) than MYSQL extension. But, it is recommended with PHP 4.1 or higher( source php.net)

The queries per second data were obtained from MySQL Administrator. You may also download one for yourself for free.

These tests may not be the actual case scenario with the webhosting servers, but still if this piece of information is helpful to you in any way, I will be glad :D. The reason that I conducted these tests was that I wanted to have the actual performance of MySQL with PHP and not just using any MySQL tool to query the database server directly.


Test

The sheet has been prepared for 5000 queries and 2000 queries. The php script looped through the queries to obtain the desired operation. The script execution time displayed in the browser is the summation of all the query time and other script execution time.
total time = database query time + other script execution time

The data in the sheet is in the following format:

max queries per sec / min queries per sec / avg queries per sec / time in seconds


The Result Sheet




Fig 3. The result Sheet
(time in seconds)










These results are for reference only. The actual results will vary from system to system and the network parameters.