Pages

Monday, February 21, 2011

Bitly using Mashape

Mashape is an API marketplace where service providers and individual developers can instantly make available an API.Its FREE to use.

Mashape can used in 2 ways

1. Making use of the featured API’s available in Mashape
2. To generate our own API using Mashape.We can upload our API in Mashape & push it into cloud.They are providing the infrastructure for generating the API.

I tried out the first way, which I need currently to work on
Check the featured services provided by Mashape -Explore.
They are in ready to use state.Avaiable in Ruby, Java, Python, PHP Obj-C, and JSON

I tried out with SimpleGeo and Bitly.

Steps to work on Bitly using Mashape :

Step1: SignIn Mashape.Get the API key.It's an automatically generated key which allows you to use the Mashape API from an application. [Ex:API key will be like this APP Key : sgFFDCfgEOKsLMsO9X7l75gCI]


Step2: Download the featured Bitly API from Mashape .
The Jar will contain Bitly component's Java client library will contain the following files:

- mashapeClient.jar - This file to be include in the project, that contains the core
functionalities of every Java client library and that is
immutable for every component.
- Bitly.java - The auto-generated *.java file for the component, that
declares a Bitly class.
- LICENSE - README

Step3 Download the following jars to work on all the above API's using Mashape.

- httpclient-4.0.jar
- httpcore-4.0-beta3.jar
- json-rpc-1.0.jar
- json-20080701.jar

Step4 Signup in Bitly & get the bitly-login and bitly-Api-key.

Step5 Code:

//Enter Mashape API key
Bitly bitly=new Bitly("sgFFDCfgEOKsLMsO9X7l75gCI ");

//Pass the url to be shortened, bitly-login and bitly-Api-key &it returens the JSONObject

JSONObject bitlyResponse = bitly.getShortenedUrl("http://sundarishree.blogspot.com/","shree","R_08de4b3475dd5655818230c15c4966471");


Advantage of this Mashape is no need to download the API jar from different sites. We can get in one point.

Monday, February 7, 2011

Session in PHP

This post might help the newbie to work on session in php.

1. Start up the session

session_start()
Function must appear before the html tag.Its initialize session data
Example :


.
.


2. Store a session variable
Example:

session_start();
$_SESSION['username']="Misty"; // store session data
?>

echo "Name =". $_SESSION['username']; //retrieve session data
?>


Where ever we try to access[Store or Retirve] the session variable the
session_start() function has to be called once before accessing the
session variable.

3. To delete the session data

The unset() function is used to free the specified session variable:
Example:

unset($_SESSION['username']);
?>


4. To destroy the session
We can completely destroy the session by calling the session_destroy()
function
Example:

session_destroy();
?>

session_destroy() will reset the session and We will lose all the stored session
data.

5. To check whether the session variable is null
Example:

is_null($_SESSION['username'])==true


6. Some of the Session Functions
session_cache_expire — Return current cache expire
session_cache_limiter — Get and/or set the current cache limiter
session_commit — Alias of session_write_close
session_decode — Decodes session data from a string

session_encode — Encodes the current session data as a string
session_get_cookie_params — Get the session cookie parameters
session_id — Get and/or set the current session id
session_is_registered — Find out whether a global variable is
registered in a session
session_module_name — Get and/or set the current session module
session_name — Get and/or set the current session name
session_regenerate_id — Update the current session id with a newly
generated one
session_register — Register one or more global variables with
the current session
session_save_path — Get and/or set the current session save path
session_set_cookie_params — Set the session cookie parameters
session_set_save_handler — Sets user-level session storage functions
session_unregister — Unregister a global variable from the
current session
session_write_close — Write session data and end session
7. To check the page expiry using session
Example:

session_start();
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
error_reporting (E_ALL);
session_cache_expire( 20 );
$inactive =250;
if(isset($_SESSION['start']) ) {
$session_life = time() - $_SESSION['start'];
if($session_life > $inactive){header("Location:login.php?errorMsg=Your session
has expired.Please login again.");
}
}
$_SESSION['start'] = time();
?>

This code has to placed in the pages in which we need to check for session
expiry [Session expires beyond 4 mins].


8. ERRORS -> While working on sessions in PHP you might
come across
the following errors. I got these 2 errors.

8.1 Warning: session_start() [function. session-start]: Node no
longer exists in C:\wamp\home.php on line 3

occurs if a object is taken from a xml file or format.

Cannot serialize object wrapping 3rd party library structs. Must serialize
the xml (to a string) and store that to session and reload the xml when
restoring from session
Example

session_start();
/*To get from the mail.xml */
$xml = simplexml_load_file("../mail.xml");
foreach($xml->children() as $child){
if($child->getName()=='servername'){
$servername=$child;
}else if($child->getName()=='dbname'){
$dbname=$child;
}else if ($child->getName()=='username'){
$username=$child;
}else if ($child->getName()=='password'){
$password=$child;
}else if ($child->getName()=='url'){
$url=$child;
}
}
$_SESSION['url']=(string)$Url; // Casting is required


8.2. The following error might occur when we try to re-direct the page from one
page to another.

Warning: Cannot modify header information – headers already
sent by (output
started /home/kvel/kyybaventures.com/laton_v/views/leftnav.php:28) in
/home/kvel/kyybaventures.com/laton_v/views/welcome.php on line 10


The problem is mainly due to the header start writing the output and again
trying to resent the header information. Normally when we use echo statement
before the header, this type of problem will come. It will not reflect in
the development environment and if try to deploy the code in the remote
server, this problem used to occur. Even if you remove the echo statement
and still the problem persist, follow the below solution.

To get rid of this error use these two lines

  

at the start of the php file and

 

at the bottom of the php file where you got the above error.