28 February, 2011

Working with question mark in url in codeigniter routing

Once i was working with Paypal payment integration (subscriptions) with codeigniter. Got the subscription part working but i was having problem when paypal redirected to my site after a successful payment.

My return url after successful payment from paypal was controller_name/get_success,
but Paypal added some extra info in this url and made it like:
Since i had used

$config["enable_query_string"] = FALSE;

this caused me problem, it was showing a 404 page. I tried fixing this problem with routing like,

$route["controller_name/get_success\?(:any})] = "controller_name/get_success";

but this didnt work at all.
So, after some searching, i came up with a solution, and it was using hooks. Though we could use pre_controller hooks, but i opted using a pre_system hook, as:

Step 1:
//Inside application/config/config.php

$config['enable_hooks'] = TRUE;

Step 2:
//inside application/config/hooks.php

$hook['pre_system'] = array(
'function' => 'remove_the_stuff',
'filename' => 'remove_get.php',
'filepath' => 'hooks'

Step 3:
Created remove_get.php file inside application/hooks

function remove_the_stuff() {
if (isset($_GET['auth'])) {
define('AUTH', $_GET['auth']);

Step 4:
Set up the uri_protocol in application/config/config.php

$config["uri_protocol"] = "REQUEST_URI";

And thats it, its working without any problems.Hope this is useful.

15 February, 2011

Login to Hyves using cURL

Logging in to hyves using the API provided by them are great, but i had a different situation where i needed to login to hyves using curl. For this i googled through
but could not get any resources. So i thought of creating it on my own.

The code connects to hyves with curl. Its simple, we just need to use appropriate curl options getting hidden field name::value pairs from the hyves login page along with the form action. And we are done.

So here's the code.

//function to get hidden field name:value pairs from html page
function getHidden($formAsString) {
$doc=new DOMDocument();
$xpath=new DOMXPath($doc);
foreach($hidData as $field) {
//type cast the value to string
$name=(string) $field->getAttribute('name');
$value=(string) $field->getAttribute('value');
return $hidEles;
//hyves mobile page url for login
$hyvesUrl = "http://www.hyves.nl/mini/?l1=mo";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $hyvesUrl);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPGET ,true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_REFERER, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$homepage = curl_exec($ch);

//get the action value of the login form
$searchStr = "/class=\"form\" action=\"(.*?)\"/";
preg_match($searchStr, $page, $matches);
$frmAction = $matches[1];

//get the hidden field name:value pairs from login page

//prepare post values
$flag = false;
foreach ($eles as $name=>$value) {
if ($flag)
$flag = true;

//now post the login form
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $form_action);
curl_setopt($ch, CURLOPT_POSTFIELDS,$els);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resLogin = curl_exec($ch);
echo $resLogin;
//and you are loggedin

So, in this way you can login to hyves using curl, and after logging in you can do
lots of other stuffs, like add new tips, blogs, send message, etc.

14 February, 2011

How to get all hidden elements from a form using php

I always had problem getting all the hidden elements of a form when using cURL. So thought of making a function that extracts all the hidden elements from a form when passed as a string.
Here's the function

function getHidden($form) {
$doc=new DOMDocument();
$xpath=new DOMXPath($doc);

//use xPATH for quering the hidden elements in a form passes as string $form

//loop thru all the data
foreach($xData as $value) {
//typecast the name and values as string to avoid xml object
$eleName=(string) $value->getAttribute('name');
$eleValue=(string) $value->getAttribute('value');
return $hiddenElements;


And thats it. The above function can be useful while doing a cURL using php to get form fields. Hope this helps someone

04 February, 2011

Facebook Share using cURL & PHP

I required a php script for facebook share using curl, found some but none of them worked for me. Found one script that was close to working, so i changed and added some codes in it and finally got it working. So following code snippet can be used for facebook share using curl in php.

The following code uses m.facebook.com for using the share feature. It first logs in a valid facebook user, gets the page after logging in, extracts hidden field values and form action used for posting message, then finally shares the message using curl.

$status = 'YOUR MESSAGE HERE';
$login_email = 'YOUR-EMAIL-ADDRESS';
$login_pass = 'YOUR-FACEBOOK-PASS';

//curl to login to facebook
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20070309 Firefox/");

//get the page after logging in successfully
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);

//get hidden values
$searchStr = "/name=\"post_form_id\" value=\"(.*?)\"/";
preg_match($searchStr, $page, $matches);
$post_form_id = $matches[1];

$searchStr2 = "/name=\"fb_dtsg\" value=\"(.*?)\"/";
preg_match($searchStr2, $page, $matches1);
$fbDtsg = $matches1[1];

$searchStr3 = "/name=\"charset_test\" value=\"(.*?)\"/";
preg_match($searchStr3, $page, $matches2);
$charsetTest = $matches2[1];

//get the posting url
$searchStr4 = "/id=\"composer_form\" action=\"(.*?)\"/";
preg_match($searchStr4, $page, $matches3);
$frmAction = $matches3[1];

//final post url
$postStatUrl = 'http://m.facebook.com'.$frmAction;

//finally post your message
curl_setopt($ch, CURLOPT_URL, $postStatUrl);
curl_setopt($ch, CURLOPT_POSTFIELDS,'charset_test='.$charsetTest.'&fb_dtsg='.$fbDtsg.'&post_form_id='.$post_form_id.'&status='.$status.'&update=Share');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

And thats it. Share it.

03 February, 2011

Writing Distributable Code in PHP

Several stuffs should be taken care of when you are working in a team,
or writing code for public release. Those stuffs can be:

a) Code should be easily readable.
b) Code should be extendable.

Well, that's what came to my mind for now. Debugging or extending someone else's code can be the toughest and frustrating task if the previous developer have not coded keeping above two points in mind.

So, if you want to stop troubling other's and yourself of-course, then you should make hebit of writing distributable code. The terminology 'distributable code' means nothing but following a good programming practice. At first, following a programming practice may tend to be uneasy, but as you keep on working you make it a habit.

Following are some of the programming practice that can be followed.
1)Select a Coding Standard
A coding standard can be:
a) Naming conventions (for files, variables, classes, etc)
b) Setting indentation rules
c) Commenting well and documentation

2)Writing Code in OOP
Start habit of Object oriented programming. Though experts say OOP tends to be costly to an application performance. But, comparitively they are cheaper then
web developers. Object Oriented code can be easily reused and extended. Though OOP may not give performance hike but it definitely give's you the faster better code as compared to slower better code.

So, write Distributable code and help yourself and others.