Install Phalcon PHP framework in Fedora

In my last article i discussed that how to install phalcon framework in openSUSE, and i guess it was quite fun and easy. Today i am extending the installation to Fedora. I installed it on Fedora 19, 20 and i hope there would be no problems for anyone, it will be quite simple and easy to install. As we know, we don’t have any repo or RPM at the moment for Fedora, so we will build it from source. Don’t worry it will not be scary. Read this for the dependencies 1st.

Fire up the terminal and i assume that you are in your home dir ([user@localhost ~]$). But no worries, you can change the dir to any where you want like your backup drive etc. Let’s go with home in this article. We need to clone the phalcon repository from github and create the extension from source.

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”3″ tab_size=”4″]
[user@localhost ~]$ git clone git://github.com/phalcon/cphalcon.git
[user@localhost ~]$ cd cphalcon/build
[user@localhost ~]$ sudo ./install
[/cc]

That will do the necessary for now, one last thing now. Open php.ini in terminal or whatever GUI editor you prefer(i will use vim):

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”1″ tab_size=”4″]
[user@localhost ~]$ sudo vim /etc/php.ini
[/cc]

And add this line:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”1″ tab_size=”4″]
extension=phalcon.so
[/cc]

Save and that’s it. Time to restart the server:

[cc lang=”bash” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”1″ tab_size=”4″]
[user@localhost ~]$ sudo service httpd restart
[/cc]

Download any of the app from the phalcon repo like invo OR vokuro etc, and give it a try.

Enjoy!

Pull pages data via facebook graph

In the last article we discussed how to create or extend access token for facebook pages. Today we are going a bit further and writing a script where we can pull some pages data.
So here is the full code, i will explain it below where necessary.

Save the following code as index.php(or whatever name you like).
[cc lang=”php” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”false” lines=”80″ tab_size=”4″]
<?php
// Facebook APP [Pages App for access tokens]
define(‘APP_ID’, ‘enter_app_id_here’);
define(‘APP_SEC_KEY’, ‘enter_app_secret_here’);

// Access Token
define(‘ACCESS_TOKEN_PAGE1’, ‘enter_access_token_here_for_page1’);
define(‘ACCESS_TOKEN_PAGE2’, ‘enter_access_token_here_for_page2’);

// Page IDs
define(‘APP_PAGE_ID_PAGE1’, ‘enter_page1_id_here’); // for page1
define(‘APP_PAGE_ID_PAGE2’, ‘enter_page2_id_here’); // for page2

// Create array for pages ids, tokens, country etc
$total_pages = array( array( ‘Pageid’ => APP_PAGE_ID_PAGE1, ‘Access_token’ => ACCESS_TOKEN_PAGE1, ‘Country’ => ‘FINLAND’ ), array( ‘Pageid’ => APP_PAGE_ID_PAGE2, ‘Access_token’ => ACCESS_TOKEN_PAGE2, ‘Country’ => ‘SINGAPORE’ ) );

// periods
$day = ‘day’;
$week = ‘week’;

$date = date(“10-07-2013”);

foreach ($total_pages as $pages) {

$pageid = $pages[‘Pageid’];
$accesstoken = $pages[‘Access_token’];
$country = $pages[‘Country’];

// Daily People Talking About This [page_storytellers]
$fql_query_page_storytellers = ‘https://graph.facebook.com/’.’fql?q=SELECT+metric,value+FROM+insights+WHERE+object_id=’.$pageid.’+

AND+metric=”page_storytellers”+AND+end_time=end_time_date(“‘.$date.'”)+

AND+period=period(“‘.$day.'”)&access_token=’.$accesstoken;

$fql_obj_page_storytellers = file_get_contents($fql_query_page_storytellers, true);

//print_r($fql_obj_page_storytellers);
if(!empty($fql_obj_page_storytellers[‘data’])) {
echo $e1 = $fql_obj_page_storytellers[‘data’][‘0’][‘value’];
}
else
{
echo $e1 = “empty string”;
}

// Daily New Likes [page_fan_adds_unique]
$fql_query_page_fan_adds_unique = ‘https://graph.facebook.com/’.’fql?q=SELECT+metric,value+FROM+insights+WHERE+object_id=’.$pageid.’+

AND+metric=”page_fan_adds_unique”+AND+end_time=end_time_date(“‘.$date.'”)+

AND+period=period(“‘.$day.'”)&access_token=’.$accesstoken;

$fql_obj_page_fan_adds_unique = file_get_contents($fql_query_page_fan_adds_unique, true);

//print_r($fql_obj_page_fan_adds_unique);
if(!empty($fql_obj_page_fan_adds_unique[‘data’])) {
echo $e2 = $fql_obj_page_fan_adds_unique[‘data’][‘0’][‘value’];
}
else
{
echo $e2 = “empty string”;
}

}
?>
[/cc]

———————————————————–

PLEASE READ BEFORE USING THE SCRIPT

From the above script change the following vars:

enter_app_id_here = change this to your app id which you created.

enter_app_secret_here = enter the app secret here.

enter_access_token_here_for_page1 = enter the access token for page 1 if you have many pages.

enter_access_token_here_for_page2 = enter the access token for page 2 if you have many pages.

Note: You can add many pages as you want, but if you have only one page, just remove the 2nd array from the $total_pages array for page2. I have added country var to the array, this is only to recognize the data if have many pages while easy to save to database for later use.

enter_page1_id_here = You can get this while following the previous article.

enter_page2_id_here = You can get this while following the previous article.

For the periods, please read the facebook insight page for more details and usage.

There are not much changes required except from the above. But if you want to change the script for your need, go ahead and change/enhance it as it fits your requirements. You can pull more data by reading the insight page OR can save the data to database for later use.

———————————————————–

Open your browser and call index.php. One more thing, facebook does not provide latest data for pages so you have to go back last 3 days, if today is 14-07-2013, you can get the data for 10-07-2013 or maybe 11-07-2013.

Newsletter plugin for WordPress

Not new to WordPress, but to be precise this is my 1st plugin for public release. Wrote themes and worked on WordPress quite closely. Some of the themes i published for public consumption. There are many newsletter plugins around so what’s new in this. Good question, well there might not be some thing very new but there are some things which make me to wrote this simple plugin. It is a simple plugin with Admin option page to download the user data in csv format for selected dates. I was not after many options, i wanted a simple plug which just work the way i want it and this is why i wrote this plug from scratch. Want to read more details, no worries read them here. Installation and usage is written there in details too. Tested with WP 3.5, but should work with 3.0+ versions.

Enough of reading, lets see it in action. Sorry there is no live demo at the moment but these screen shots will do the justice.

In-action-in-theme

In action

In-action-in-wp-admin

Admin options page

Time to download and install it, head over to githug and download or pull the files. Got issues or need help, there is an issue page on github for it post it over there.

The directory name should be mma-newsletter. Remove other words, hyphens etc after extracting.

Download it from github

Thanks for using it. 🙂

Speed up your website loading time by using PHP APC [Part-5]

In some cases if you are running WordPress OR some other database extensive application on micro instance, you will experience slow page loading on various occasions.
There is a nice solution to that, install PHP APC to cache most the PHP code for fast loading.

I assume that you are already on the server console through SSH:

sudo su

1st let’s install few needed dependencies with php pear:

yum install php-pear httpd-devel php-devel

Now time to install APC by pecl command:

pecl install apc

Add a line to apc.ini to load the module:

echo “extension=apc.so” > /etc/php.d/apc.ini

Restart the httpd server:

service httpd restart

And that’s it, you will notice quite a difference now. The execution time per page will now reduce dramatically. I have tested it on a micro instance for a WordPress site, and i can confirm that the site loads fast now.

Continuation:

Part-1: Setup web server on Amazon AMI or CentOS

Part-2: Backup MySQL data into CSV

Part-3: Backup MySQL data into CSV with PHP

Part-4: Backup and Restore MySQL databases

Backup MySQL data into CSV with PHP [Part-3]

In the previous article we discuss how to “Backup MySQL data into CSV” in terminal. In this post we will discuss how to create csv file with PHP and force it to download. We will be pulling data from MySQL database. 1st of all we will need connection to database, for that we will use this script. Change the necessary details and save it as conn.php. Now lets start with the csv.

[cc lang=”php” escaped=”true” nowrap=”false” noborder=”true” line_numbers=”true” lines=”40″ tab_size=”4″]
<?php
require_once ‘conn.php’;

$sql = $dbh->prepare(“SELECT u.name, u.email, u.phone, u.city, s.filename FROM_UNIXTIME(s.sub_time, ‘%M %D %Y – %l:%i %p’) from users u join submissions s on u.uid=s.uid where s.uid != ” and s.filename != ” order by s.sub_time desc”);
$sql->execute();
$row = $sql->rowCount();

if ($row > 0){

$columns = array(‘NAME’,’EMAIL’,’PHONE’,’CITY’,’FILENAME’,’TIME’);

$filep = fopen(‘php://output’, ‘w’);
if ($filep) {
$file = uniqid().’_’.time().’.csv’;
header(“Content-Type: text/csv”);
header(“Content-Disposition: attachment; filename=$file”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
fputcsv($filep, $columns);

while ($rows = $sql->fetch(MYSQL_NUM)) {
fputcsv($filep, $rows);
}
die;
}
}
?>
[/cc]
The script is quite straight forward and simple to use/follow. Little bit explanation: we are joining two tables here and then checking if any record found with rowCount function, if so we proceed to create csv. The array store the headers which will be column headers in the csv file. Giving a unique name to the file we will download and then using PHP fputcsv function to write data, quite easy and painless.

Continuation:

Part-1: Setup web server on Amazon AMI or CentOS

Part-2: Backup MySQL data into CSV

Part-4: Backup and Restore MySQL databases

Part-5: Speed up your website loading time by using PHP APC

Setup web server on Amazon AMI or CentOS [Part-1]

If you are using Amazon aws with ec2 instances, then i assume you already have the pem file to connect to the instances. Normally you will have different instances for your projects. I normally setup either Amazon AMI or Cent OS, prefer Amazon AMI as it is also based on Cent OS. Cent OS is best and easy OS for servers. I love the configuration and level of interaction it provides.
Let’s say you want to install a web server with MySQL and PHP, run the following command:

sudo yum install httpd mysql-server php php-mysql mysql php-xml php-pdo php-odbc php-soap php-common php-cli php-mbstring php-bcmath php-ldap php-imap php-gd mod_ssl

That will install all the necessary modules and packages to run a fully functional server.
In some cases we don’t need/want a web server, just database server will do it:

sudo yum install mysql-server mysql

That will only install database server on the instance.
Now let’s say we dont need either PHP or MySQL:

sudo yum install httpd

Some time we don’t need database server on the same web server instances:

sudo yum install httpd php php-xml php-mysql php-pdo php-odbc php-soap php-common php-cli php-mbstring php-bcmath php-ldap php-imap php-gd mod_ssl

That will install all necessay packages excluding MySQL.

Now if you have a MySQL server, for sure you would like to make it secure, that is easy:

sudo /usr/bin/mysql_secure_installation

The above command will ask you some questions, answer the questions carefully and you will get through it.
Don’t forget to remove anon users and test database while running the above command.

If you have .htaccess file and want to utilize mod rewrite, enable it in /etc/httpd/conf/httpd.conf in the server directory section:

AllowOverride None
to
AllowOverride All

To auto restart your server and database server in case the system is restarted:

sudo chkconfig httpd on
sudo chkconfig mysqld on

Manually start the servers:

sudo service httpd start
sudo service mysqld start

Some interesting information i will share in upcoming articles: Load balancer for multiple instances, configuring SSL on load balancer, creating databases and changing password, Dumping databases and saving table/database to csv and save only database schema. And separating your database server from web servers with load balancer and adding the instances to database for security purpose to read/write into database instance.

So stay tune, there are more and plenty to come.

Continuation:

Part-2: Backup MySQL data into CSV

Part-3: Backup MySQL data into CSV with PHP

Part-4: Backup and Restore MySQL databases

Part-5: Speed up your website loading time by using PHP APC