PHP search facebook posts using Graph API (JSON)

in PHP

Facebook offers a flexible and easy to use API called Graph, graph is simply a URL that once you open in a browser or within your code it return a JSON (JavaScript Object Notation ) string, in this tutorial you will learn how to use php and facebook graph API to access public posts and parse JSON results with php, We will build together a php  search engine to parse JSON returned by Facebook Graph and display results to visitors

view this in action Demo

If you don’t know what is Json, a Json string looks like this

{
“name”: “John Smith”,
“Age”: “30″,
“username”: “Jsith”,
“location”: “Chicago”
}

Our search engine will be 2 pages

index.html
search.php

index.html content

<form action=”search.php” method=”post”>
<input type=”text” name=”keyword” />
<input type=”submit” value=”search” />
</form>

in the above step we build a basic html page with a form that contains text field and submit button to submit the form to our search.php, then once a user type a keyword and hit submit we want to take that keyword and search facebook public posts suing their graph API.

Now in search.php We need to catch the submitted keyword from index.html

$keyword = $_POST['keyword'];

The graph url which will return a JSON string is https://graph.facebook.com/search
the url accepts many parameters but we will use the basic ones such as type and q (query)
you can see what the JSON results would look like before we parse them with PHP here:
https://graph.facebook.com/search?q=hello&type=post
Now lets set the graph url we will be opening with php

// set graph url
$graph_url = “https://graph.facebook.com/search?”;
// add parameters to graph url
$graph_url .= “&type=posts”;
$graph_url .= “&q=$keyword”;

in the above step we have a prepared graph url with type parameter and q parameter, the q parameter is the keyword we want to search for and type is the kind of contents we need, we are going to search for posts, aren’t we?
Now that facebook graph url is ready with the keyword we want to search for, lets have php open it so it returns the JSON results

$results = file_get_contents( $graph_url );


$result
will be the variable holding the JSON data returned by Facebook Graph, We now need to parse JSON and display it as readable text or HTML content, We will parse it using php function – json_decode() -

$json = json_decode($results);

the json_decode() function in the way used above converts the JSON string into accessible objects, We will need to do a foreach loop to access those objects and print them, lets do that now

foreach( $results->data as $show ) {
echo $show->from->name . “\n”;
echo $show->message . “\n”;
echo $show->created_time . “\n”
echo “<hr>>”;
}

Now you might be wondering why used $json->data and not $json alone, the answer is because all the returned results in JSON are inside an array called “data” and also you may have noticed to show the name of poster I’m doing $json->from->name is because “from” is an array and name is an object inside that array you can see how it looks like here https://graph.facebook.com/?q=hello&type=post, In this tutorial I’m only showing name of person who made the post, message which is post content, created time is the timestamp of the post date you can access many more such as Facebook user ID , description, comments count etc. By simple doing $show->xx where xx is the object name! I hope this helps you in your next Facebook app project! you can what we have created in action here

search.php all together will look like this

$keyword = $_POST['keyword'];
$graph_url = “https://graph.facebook.com/search?”;
$graph_url .= “&type=posts”;
$graph_url .= “&q=$keyword”;
$results = file_get_contents( $graph_url );
$json = json_decode($results);
foreach( $results->data as $show ) {
echo $show->from->name . “\n”;
echo $show->message . “\n”;
echo $show->created_time . “\n”
echo “<hr>>”;
}

0 Comments

CodeIgniter time ago – How to get it done!

in PHP

In today’s web, its much better to use x hour ago, day ago , month ago , a year ago etc. CodeIgniter has a built-in function in date_helper that allows you to convert any date/time to “xx Time ago” style, its called timespan() it takes 2 arguments the first one is the date/time you want to convert to “time ago style” and the second one should be current date time, below is an example show you how to use CodeIgniter timespan function

First you will need to load the date helper

$this->load->helper(‘date’);

Now lets use the function in any of our controllers

timespan(1326927423, time() ) . “ago” ;

That will outputs something like

2 hours ago

Note that 1326927423 is a UNIX date format, see this page to learn about date formats and how to convert them.

0 Comments

How to install OpenVPN Access server on a VPS

in Servers

Let install OpenVZ AS (Access server) on an openvz VPS, as most vps providers are using openvz technology.
We will be installing openvpn AS on a VPS with centOS 6 32bit, It should be no problem if you are using 64bit also this tutorial can work just fine on a dedicated server or Xen VPS.

The first thing you should do if you are on a openVZ VPS is asking your vps provider to enable TUN/TAP device for you or just give them the following commands to run on their hardware node
to enable TUN/TAP

vzctl set 101 –devnodes net/tun:rw –save

to enable IP tables if necessary

vzctl set 101 –iptables ipt_REJECT –iptables ipt_tos –iptables ipt_TOS –iptables ipt_LOG –iptables ip_conntrack –iptables ipt_limit –iptables ipt_multiport –iptables iptable_filter –iptables iptable_mangle –iptables ipt_TCPMSS –iptables ipt_tcpmss –iptables ipt_ttl –iptables ipt_length –iptables ipt_state –iptables iptable_nat –iptables ip_nat_ftp –save

where 101 is your vps CID.

Now ssh to your server, if you are on Linux (Ctrl + t) to open terminal and type inside

ssh root@xxx.xxx.xxx.xx

Where xxx.xx.. is your server IP address, it will then ask for root password just type it and hit enter to login to your server.

If you are on windows then use putty to connect to your server.
now that you’re logged in, run the following command to download openvpn access server installation script
for centOS 32bit do this

wget http://swupdate.openvpn.org/as/openvpn-as-1.8.3-CentOS5.i386.rpm

for centOS 64bit do this

wget http://swupdate.openvpn.org/as/openvpn-as-1.8.3-CentOS5.x86_64.rpm

run Openvpn Access Server installer for (32bit)

rpm -i openvpn-as-1.8.3-CentOS5.i386.rpm

run Openvpn Access Server installer for (64bit)

rpm -i openvpn-as-1.8.3-CentOS5.x86_64.rpm

it will take anywhere between 4 minutes and 10 minutes, it will create the master username(openvpn) so once done lets change the user “openvpn” password by running this

passwd openvpn

choose a password, type and confirm it and that is it! you got openvpn access server installed and ready, to login to openvpn access server admin, the url would be something like this

https://xxx.xxx.xxx:943/admin

where xxx.xxx.xxx is your server IP address, and make sure the url is starting with https, the user name would be openvpn and password would be the one you have chosen in last step.

and to login as a client the url would be

https://xxx.xxx.xxx

the first time a client connect to openvpn access server, he may be asked to download openvpn connect client.

0 Comments

How to host Multiple domains/Websites on Apache Without Control panel

in Servers

So you have got a dedicated server or VPS and want to host more than one website /domain on Apache web server but also don’t want to spend money on control panel like cPanel , Directadmin etc.. I will show you how to seriously host multiple websites or domains on your LAMP stack easily without any control panel! in this small tutorial I have Apache with Mysql , PHP on Ubuntu.

then lets SSH to server first, I’m connecting from my Ubuntu desktop, if you are on windows then look at putty a cool ssh client for windows computers

ssh root@xx.xxx.xx

in this example we are going to host the domain name “www.example.com”.
now that you are logged in to server, lets change dir

cd /etc/apache2/sites-available

let’s create a configuration file for www.example.com
NOTE: I will be using “vi” its a text editor that comes with Ubuntu and you can use it in any ssh session, if you don’t like it then install yum or just sftp to server and work with a GUI

vi www.example.com

Now that vi has create the virtual host file , lets copy and paste the following inside that file
TIP to paste the following in the file we created/opened in the step above, press i then mouse right click


ServerAdmin name@example.com
ServerName example.com
ServerAlias www.example.com
# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /var/www/example.com

Now lets save that file, press Esc 2 times then type:

:wq

Now we have to enable www.example.com on the server, to do that issue:

a2ensite www.example.com

Great! Now you only need to go to /var/www to create a home directory for example.com, remember in the steps above we created a configuration file that has a virtual host for example.com? in that file we also added something like DocumentRoot ?
thats where the domain home directory will be so lets create it.

cd /var/www
sudo mkdir example.com

Lets reload apache

service apache2 reload

Now remain to add an A record to example.com wherever you have it registered, here is a guide on how to do that on Godaddy

That is it! just upload the website files to /var/www/example.com and you will be able to access them by visiting wwww.example.com

4 FREE CodeIgniter based CMS

in Resources

This is a list containing FREE CMS ( Content management systems ) which were built on the popular PHP Framework Codeigniter , We have tried to find every free CMS out there, if you think we missed some, Please share them with your comment!

FuelCMS

Features:

  • Inline Editing
  • Simple Interface
  • Import Existing Static Pages
  • Manage Users & Permissions
  • 3rd Party Integration
  • Cronjob Manager
  • Roll a Blog

CMS Installer: NO

Templates: Yes

 

CodeFight

Features:

  • Pages manager
  • Multi Website
  • Users Groups
  • Menu manager

CMS Installer: NO

Templates: NO

 

AmarokCMS

Features:

  • WYSIWYG html editor
  • Image/File upload
  • page SEO options
  • Page Sidebar
  • Custom slugs
  • Multi-Level menu
  • Page menu orders

CMS Installer: Yes

Templates: Yes


Ionize CMS

Features:

  • Easy edition
  • Multilingual
  • Userfriendly
  • Template System
  • Extend Content
  • Articles
  • Page menus

CMS Installer:

Templatable: Yes

best CSS Frameworks for building Forms, Buttons & messages

in Resources

Twitter Bootstrap is a toolkit from Twitter It includes base CSS and HTML for typography, forms, buttons, tables, grids, multi-level navigation, success messages, warnings messages and error messages and much more
twitter-bosstrap-css-framework

Blueprint is a CSS framework that gives you a solid foundation to build great looking websites! Blueprint offers the following

  • A CSS reset that eliminates the discrepancies across browsers.
  • A solid grid that can support the most complex of layouts.
  • Typography based on expert principles that predate the web.
  • Form styles for great looking user interfaces.
  • Print styles for making any webpage ready for paper.
  • Plugins for buttons, tabs and sprites.

blueorint-framework-warning-messages

formee is a veyr cool css framework that allows you to create very sexy web forms and warning/success messages

formee-css-forms-framework

52Framework is a CS3 framework that features box shadows, border radius, text shadow and box/text/rotating and more

52framework preview

 

ImportError: No module named tkinter [solved]

in Ubuntu

Today I have been looking at http://en.wikibooks.org/wiki/Python_Programming/User_Interaction a quick introduction to Python Graphical User Interface (GUI) development and following their simple tutorial to create a small program that takes user input I get the following

ImportError: No module named tkinter

Python has many different GUI toolkits, of which the most standard is Tkinter which comes with Python but I was unable to get tkinter to work so I Googled a bit and found some articles claiming that running the following commands can fix the problem

sudo apt-get install tk-dev

or

sudo apt-get install python-tk

But it really did not solve the problem and I kept getting

ImportError: No module named tkinter

Until I found out that this is wrong

import tkinter or tkinter.Tk() etc…

but you must always use capital “T” in tkinter :) Like this:

import Tkinter

app_win = Tkinter.Tk()

that solved my problem and I hope it solves yours!
Oh my python version is 2.7.2+

Migrating Thunderbird from Windows to Ubuntu

in Ubuntu

So do you want to have all your email profiles and received emails/files on your new Ubuntu installation? There is a very easy way to migrate Thunderbird from windows to Ubuntu, in my case I migrated from Windows 7 to Ubuntu 10.11 but I think it should be the same way for Windows vista or XP. also its good to note that this article can be applied to Firefox Windows to Ubuntu profile migration as well.

if you have Ubuntu and Windows side by side

Boot with ubuntu then go to your Home folder and on the left you will see under “Devices” the hardisk your windows installation is on, Click on that and go to Documents & Settings > Your_user_name > Application & Data > Thunderbird > Profiles

there should be a folder named something like  xxxx.default copy that folder and go back to your Ubuntu home directory press Ctrl+h to show hidden folders and then find a folder called .thunderbird open it and paste there the folder you copied in the steps above

 

Wait…. Do not leave that folder yet.

open up the file called profiles.ini you should see something like this:

[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=xxxxx.default

change the xxxxx.default to the copied folder’s name and start Thunderbird and you should see all of yours emails there.

if you don’t have Ubuntu and Windows side by side then you will need to put that folder on a USB stick and use it in Ubuntu

How to reserve wordpress comments to show newest to oldest

in Wordpress

Its very simple to reserve wordpress comments and show the newest comments first , you can do that for top level comments or child comments, changing wordpress comments order is so simple, open up your theme comments.php file and find the following:


and replace it with this to show newest comments at the top and then older comments (for top level only)


To do that for child comments also then replace it with the folowing:


if you theme has something like this instead:


Then replace it with this:


If you have any question please post it here! and don’t forget to bookmark tutorialsbay

web 2.0 PSD email form

in Resources

FREE PSD email / contact us form from Tutorialsbay.com
psd-contact-form

Download PSD form