Showing posts with label CURL. Show all posts
Showing posts with label CURL. Show all posts

02 April, 2011

Login to Yahoo mobile using curl

Once i had to login to yahoo mobile for some calendar events manipulation, but since yahoo does not have any api for doing such stuffs so thought of giving it a try using curl. After some tests and hit & triali was able to login yo yahoo mobile
using curl and do my necessary calendar events manipulation.
Below is the code snippet for logging in to yahoo mobile using curl.

$mCookie = "mcookie.cookie";
$reffer = "http://m.yahoo.com/calendar";
$mUrl = "http://m.yahoo.com/calendar";
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt($ch, CURLOPT_URL, $mUrl);
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $mCookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $mCookie);
$mRes = curl_exec($ch);

//get the hidden fields

preg_match_all ( "/id=\"LoginModel\" action=\"(.*)\"/", $mRes, $formActionArr);
preg_match_all ( "/name=\"_authurl\" value=\"(.*?)\"/", $mRes, $authUrlArr);
preg_match_all ( "/name=\"_done\" value=\"(.*?)\"/", $mRes, $doneArr);
preg_match_all ( "/name=\"_ts\" value=\"(.*?)\"/", $mRes, $tsArr);
preg_match_all ( "/name=\"_crumb\" value=\"(.*?)\"/", $mRes, $crumbArr);

$authUrl = $authUrlArr[1][0];
$doneUrl = $doneArr[1][0];
$ts = $tsArr[1][0];
$crumb = $crumbArr[1][0];
$signIn = urlencode("Sign In");
$username = "yahoo_username";
$password = "yahoo_password";
$loginUrl = "https://mlogin.yahoo.com/w/login/auth?.ts=".$ts."&_httpHost=m.yahoo.com&.intl=NP&.lang=en";
$postFields = "_authurl=".$authUrl."&_done=".$doneUrl."&_sig=&_src=&_ts=".$ts."&_crumb=".$crumb."&_pc=&_send_userhash=0&_appdata=&_partner_ts=&_is_ysid=&_page=secure&_next=&id=".$username."&password=".$password."&__submit=".$signIn;

//now post the hidden fields
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $mCookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $mCookie);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
$postRes = curl_exec($ch);
echo $postRes;
//we are logged in now
?>

Thats it, Hope this helps someone.

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) {
$hidEles="";
$doc=new DOMDocument();
$doc->loadHTML($string_bulk);
$xpath=new DOMXPath($doc);
$query="//input[@type='hidden']";
$hidData=$xpath->query($query);
foreach($hidData as $field) {
//type cast the value to string
$name=(string) $field->getAttribute('name');
$value=(string) $field->getAttribute('value');
$hidEles[$name]=$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
$eles=getHiddenElements($homepage);
$eles['auth_username']="hyves-username";
$eles['auth_password']="hyves-password";
$eles['login']='Login';

//prepare post values
$els='';
$flag = false;
foreach ($eles as $name=>$value) {
if ($flag)
$els.='&';
$els.="{$name}=".urlencode($value);
$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.

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:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);

//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);
curl_exec($ch);
?>

And thats it. Share it.

28 May, 2010

Download file using curl

Following code sample is used to download file using curl. In this example we download image using curl and
save it to some file in our directory.

// URL to Download Image
$url = "http://www.someurl.com/images/image.jpg";
// Initialize a CURL session.
$ch = curl_init();
// Pass URL as parameter.
curl_setopt($ch, CURLOPT_URL, $url);
// Return contents.in a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Data is in binary format
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
//open file in write mode
$file = fopen("somedir/somename.jpg", "w");
//filename that the transfered binary data is returned to
curl_setopt($ch, CURLOPT_FILE, $file);
//Grab the jpg and save the contents in the $data variable
$data = curl_exec($ch);
curl_close($ch); // close curl resource
?>