Senin, 14 Februari 2011

Send Message to Yahoo! Messenger from PHP

<?php
  // script name: send_message_to_yahoo_messenger.php
  // web scraper: Sony AK Knowledge Center - www.sony-ak.com
  // last update: July 20, 2010
 
  // get home page of yahoo mobile
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, "http://us.m.yahoo.com/w/bp-messenger");
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_ENCODING, "");
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14,52; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)");
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_yahoo_messenger.cookie');
  $curlData = curl_exec($curl);
  curl_close($curl);
 
  // debug: show the returned html
  // echo $curlData; exit;
 
  // get post url for login to yahoo
  $xml = $curlData;
  $xmlDoc = new DOMDocument();
  @$xmlDoc->loadHTML($xml);
 
  $urlPostLoginToYahoo = $xmlDoc->getElementsByTagName("form")->item(0)->getAttribute("action");
 
  foreach ($xmlDoc->getElementsByTagName("input") as $input) {
    if ($input->getAttribute("name") == "_done") {
      $_done = $input->getAttribute("value");
    }
    if ($input->getAttribute("name") == "_ts") {
      $_ts = $input->getAttribute("value");
    }
    if ($input->getAttribute("name") == "_crumb") {
      $_crumb = $input->getAttribute("value");
    }
  }
 
  // do login to yahoo messenger (mobile version)
  $yahoo_id = "your_yahoo_id";
  $yahoo_id_password = "your_yahoo_id_password";
 
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $urlPostLoginToYahoo);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, "_authurl=auth&_done=" . $_done . "&_sig=&_src=&_ts=" . $_ts . "&_crumb=" . $_crumb . "&_pc=&_send_userhash=0&_partner_ts=&id=" . $yahoo_id . "&password=" . $yahoo_id_password . "&__submit=Sign+in");
  curl_setopt($curl, CURLOPT_ENCODING, "");
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14,52; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)");
  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_yahoo_messenger.cookie');
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_yahoo_messenger.cookie');
  $curlData = curl_exec($curl);
  curl_close($curl);
 
  // get home page url for sending message
  $urlSendMessage = $curlData;
  $urlSendMessage = substr($urlSendMessage, strpos($urlSendMessage, "<a href=\"/w/bp-messenger/sendmessage") + 9);
  $urlSendMessage = substr($urlSendMessage, 0, strpos($urlSendMessage, "\""));
  $urlSendMessage = str_replace("&amp;", "&", $urlSendMessage);
  $urlSendMessage = "http://us.m.yahoo.com" . $urlSendMessage;
 
  // get home page of mobile messenger to send message
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $urlSendMessage);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_ENCODING, "");
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14,52; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)");
  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_yahoo_messenger.cookie');
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_yahoo_messenger.cookie');
  $curlData = curl_exec($curl);
  curl_close($curl);
 
  // debug: show the returned html
  // echo $curlData; exit;
 
  $xml = $curlData;
  $xmlDoc = new DOMDocument();
  @$xmlDoc->loadHTML($xml);
 
  $urlPostSendMessage = $xmlDoc->getElementsByTagName("form")->item(0)->getAttribute("action");
  $urlPostSendMessage = "http://us.m.yahoo.com" . $urlPostSendMessage;
 
  // do send message to yahoo messenger
  $yahoo_username = "sonyarianto"; // this is Yahoo! ID target
  $yahoo_message = "This is my message to you!"; // this is Yahoo! messenger message to the target
 
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $urlPostSendMessage);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, "id=" . $yahoo_username . "&message=" . $yahoo_message . "&__submit=Send");
  curl_setopt($curl, CURLOPT_ENCODING, "");
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14,52; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)");
  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd() . '/cookies_yahoo_messenger.cookie');
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd() . '/cookies_yahoo_messenger.cookie');
  $curlData = curl_exec($curl);
  curl_close($curl);
 
  echo "your message already sent to Yahoo! ID: " . $yahoo_username;
?>

PHP curl bot to update Facebook status

//
// change Facebook status with curl
// Thanks to Alste (curl stuff inspired by nexdot.net/blog)
function setFacebookStatus($status, $login_email, $login_pass, $debug=false) {
 //CURL stuff
 //This executes the login procedure
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&amp;next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
 curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=' . urlencode($login_email) . '&pass=' . urlencode($login_pass) . '&login=' . urlencode("Log in"));
 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);
 //make sure you put a popular web browser here (signature for your web browser can be retrieved with 'echo $_SERVER['HTTP_USER_AGENT'];'
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
 curl_exec($ch);

 //This executes the status update
 curl_setopt($ch, CURLOPT_POST, 0);
 curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
 $page = curl_exec($ch);

 //echo htmlspecialchars($page);

 curl_setopt($ch, CURLOPT_POST, 1);
 //this gets the post_form_id value
 preg_match("/input type=\"hidden\" name=\"post_form_id\" value=\"(.*?)\"/", $page, $form_id);
 preg_match("/input type=\"hidden\" name=\"fb_dtsg\" value=\"(.*?)\"/", $page, $fb_dtsg);
 preg_match("/input type=\"hidden\" name=\"charset_test\" value=\"(.*?)\"/", $page, $charset_test);
 preg_match("/input type=\"submit\" class=\"button\" name=\"update\" value=\"(.*?)\"/", $page, $update);

 //we'll also need the exact name of the form processor page
 //preg_match("/form action=\"(.*?)\"/", $page, $form_num);
 //sometimes doesn't work so we search the correct form action to use
 //since there could be more than one form in the page.
 preg_match_all("#<form([^>]*)>(.*)</form>#Ui", $page, $form_ar);
 for($i=0;$i<count($form_ar[0]);$i++)
  if(stristr($form_ar[0][$i],"post_form_id")) preg_match("/form action=\"(.*?)\"/", $page, $form_num);  

 $strpost = 'post_form_id=' . $form_id[1] . '&status=' . urlencode($status) . '&update=' . urlencode($update[1]) . '&charset_test=' . urlencode($charset_test[1]) . '&fb_dtsg=' . urlencode($fb_dtsg[1]);
 if($debug) {
  echo "Parameters sent: ".$strpost."<hr>";
 }
 curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost );

 //set url to form processor page
 curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com' . $form_num[1]);
 curl_exec($ch);

 if ($debug) {
  //show information regarding the request
  print_r(curl_getinfo($ch));
  echo curl_errno($ch) . '-' . curl_error($ch);
  echo "<br><br>Your Facebook status seems to have been updated.";
 }
 //close the connection
 curl_close($ch);
}

login and get contacts from yahoo mail with php curl

This is the script used to login into yahoo mail in an automated way. This script also fetching the contacts and email addresses from the address book.Very popular php curl library is used in this script.


set_time_limit(0);

if($_POST['login'])
{

$php_userid = $_POST['login'];
$php_password = $_POST['passwd'];


$cookie_file_path = "./cookie.txt"; // Please set your Cookie File path


$fp = fopen($cookie_file_path,'wb');
fclose($fp);
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$reffer = "http://mail.yahoo.com/";

// log out.
$LOGINURL = "http://us.ard.yahoo.com/SIG=12hoqklmn/M=289534.5473431.6553392.5333790/D=mail/S=150500014:HEADR/Y=YAHOO/EXP=1135053978/A=2378664/R=4/SIG=133erplvs/*http://login.yahoo.com/config/login?logout=1&.done=http://mail.yahoo.com/&.src=ym&.lg=us&.intl=us";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);

//1. Get first login page to parse hash_u,hash_challenge

$LOGINURL = "http://mail.yahoo.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$loginpage_html = curl_exec ($ch);
curl_close ($ch);

preg_match_all("/name=\".u\" value=\"(.*?)\"/", $loginpage_html, $arr_hash_u);
preg_match_all("/name=\".challenge\" value=\"(.*?)\"/", $loginpage_html, $arr_hash_challenge);

$hash_u = $arr_hash_u[1][0];
$hash_challenge = $arr_hash_challenge[1][0];

// 2- Post Login Data to Page https://login.yahoo.com/config/login?

$LOGINURL = "https://login.yahoo.com/config/login?";
$POSTFIELDS = '.tries=1&.src=ym&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u='.$hash_u.'&.v=0&.challenge='.$hash_challenge.'&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fmail.yahoo.com&login='.$php_userid.'&passwd='.$php_password;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);

preg_match_all("/replace\(\"(.*?)\"/", $result, $arr_url);
$WelcomeURL = $arr_url[1][0];

// 3- Redirect to Welcome page. (Login Success)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$WelcomeURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);
// echo $result;

// 4- Get Address Book.
$addressURL = 'http://address.mail.yahoo.com/?A=B';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$addressURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);


//print $result;
preg_match_all("/\"\/yab\/us\/Yahoo\.csv\?(.*?)\"/", $result, $arr_address_url);
$randURL = html_entity_decode($arr_address_url[1][0]);
preg_match_all("/id=\"crumb2\" value=\"(.*?)\"/", $result, $arr_crumb);
$hash_crumb = $arr_crumb[1][0];

// 5- show Address Book.
$addressURL = 'http://address.mail.yahoo.com/index.php';

$POSTFIELDS ='.crumb='.$hash_crumb.'&VPC=import_export&submit%5Baction_export_yahoo%5D=Export+Now';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$addressURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
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_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close ($ch);

//print $result;

} // extra code 2 if

else
{
login_form();
}