?
Solved

How to send PMs to Yahoo ids using PHP/CURL

Posted on 2009-05-02
8
Medium Priority
?
881 Views
Last Modified: 2012-05-06
Hi experts,

I need to connect to my Yahoo Messenger account and send PMs to other Yahoo ids. This has to be accomplished using PHP or CURL.

I downloaded the below class from phpclasses.org which connects to eBuddy Web Messenger(http://www.ebuddy.com/) to send messages to the desired IM.

I'm using PHP5. The original code was not working, so I made some changes(the original statements are commented out), still no luck. I know I am not posting the data correctly, but not sure what changes I should make.

I would really appreciate it if someone helped me figure out where I am going wrong here. Also, I was wondering if there is a direct way to connect my script to Yahoo IM rather than using some middle man like eBuddy?

Thanks.


<?php
 
/**
* PHP Class for send and get pm to/from yahoo account
* this class use other site <ebuddy.com> for work
* @author   Mohammad eSmaeilpour <esmaeilpoor.info@gmail.com>
* @access public
*/
 
class My_Yahoo_Messenger {
 
	/**
	* Server host
	* @access private
	*/
	var $serverHost = 'denver.ebuddy.com';
	
	/**
	* Login user name
	* @access private
	*/
	var $username	= null;
	
	/**
	* Login session
	* @access private
	*/
	var $_cookie	= null;
	
	/**
	*Connection handle
	* @access private
	*/
	var $fp_handle	= null;
	
	/**
	* Construct
	*@params:
	*	username: String Yahoo account user name
	*	password: String Yahoo account password
	*	login status: Boolean true from visible
	* @access public
	*/
	function __construct($username, $password, $inv = false)
	{
		$this->login($username, $password, $inv);
	}
 
	/**
	* Construct PHP 4
	*@params:
	*	username: String Yahoo account user name
	*	password: String Yahoo account password
	*	login status: Boolean true from visible
	* @access public
	*/
	function My_Yahoo_Messenger($username, $password, $inv = false)
	{
		$this->__construct($username, $password, $inv);
	}
	
	/**
	* Function provide login to yahoo account
	*@params:
	*	username: String Yahoo account user name
	*	password: String Yahoo account password
	*	login status: Boolean true from visible
	*@return: String cookie data
	* @access public
	*/
	function login($username, $password, $inv = false)
	{
		$inv = $inv === false ? 'FALSE' : 'TRUE';
		$username = urlencode($username);
		$password = urlencode($password);
		
		$content = "password=$password&network=yahoo&initial=FALSE&login_network=yahoo&username=$username&pwd=&init_status=$inv";
		
		//$this->_createConnection('POST', '/vo074922/login.jsp', 'http://www.ebuddy.com/?web_messenger', "yahoo_uname=$username; Emessenger=yahoo; network=YAHOO;", $content, false);
 
		$this->_createConnection('POST', '/vo7.3.11/start.html', 'http://www.ebuddy.com/?web_messenger', "yahoo_uname=$username; Emessenger=yahoo; network=YAHOO;", $content, false);
			
		$headers = array();
		$content = '';
        
		$atStart = true;
        $atHeader= true;
		while (!feof($this->fp_handle)) {
    	    $line = fgets($this->fp_handle, 4096);
    	    
    	    if($atStart) {
    	    	$atStart = false;
    	    	if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
    	    		break;
    	    	}
    	    }
    	    if ($atHeader) {
    	        if (trim($line) == '') {
    	            $atHeader = false;
    	            continue;
    	        }
    	        if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
    	            continue;
    	        }
    	        $key = strtolower(trim($m[1]));
    	        $val = trim($m[2]);
    	        if (isset($headers[$key])) {
    	            if (is_array($headers[$key])) {
    	                $headers[$key][] = $val;
    	            } else {
    	                $headers[$key] = array($headers[$key], $val);
    	            }
    	        } else {
    	            $headers[$key] = $val;
    	        }
    	        continue;
    	    }
    	    $content .= $line;
        }
        
        foreach ($headers as $name=>$header)
		{
        	switch (true) {
        		case strpos($name, 'location') !== false :
        			if(preg_match('@time\s*=\s*([0-9]+)@i', $header, $m)) {
        				$this->time = $m[1];
	        		}
	        		if(preg_match('@hash\s*=\s*([a-z0-9]+)@i', $header, $m)) {
	        			$this->hash = $m[1];
	        		}
        			break;
        		case strpos($name, 'set-cookie') !== false :
        			if(is_array($header)) $header = implode(';', $header);
					$this->_cookie = $header;
        			break;
        	}
		}
		@fclose ($this->fp_handle);
		
		$this->username = $username;
		
		return $this->_cookie;
	}
	
	/**
	* Function for send message to fav. yahoo account
	*@params:
	*	to: String Yahoo account
	*	message: String Your message
	* @access public
	*/
	function sendMessage($to, $message)
	{
		$message = urlencode($message);
		
		$message = preg_replace( '!<br.*>!iU', "\n", $message );
		
		$hash = isset($this->hash) ? $this->hash : '';
		$time = isset($this->time) ? $this->time : time();
		
		$content = "e_hash=$hash&e_action=send_message&e_user=$to&e_message=$message&e_format=FN%3DVerdana%3B%20EF%3D%3B%20CO%3D000000%3B%20CS%3D0%3B%20PF%3D00%3B%20RL%3D0&e_network=YAHOO&_=";
		
		//$this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo074922/main.jsp?hash=$hash&u=$this->username&network=YAHOO&time=$time", $this->_cookie, $content, false);
 
		$this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo7.3.11/main.html?hash=$hash&u=$this->username&network=YAHOO&time=$time", $this->_cookie, $content, false);
	}
 
	/**
	* Function for get unread  message from yahoo account
	*@return : Array From:Message
	* @access public
	*/
	function getMessage()
	{
		$content = "e_format=short&e_action=poll&e_time=0&e_timeout=10000&e_max=100&_=";
		
		$hash = isset($this->hash) ? $this->hash : '';
		$time = isset($this->time) ? $this->time : time();
		
		//$this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo074922/main.jsp?hash=$hash&u=$this->username&network=YAHOO&time=$time", null, $content, false);
 
		$this->_createConnection('POST', '/dispatch', "http://".$this->serverHost."/vo7.3.11/main.html?hash=$hash&u=$this->username&network=YAHOO&time=$time", null, $content, false);
		
		$reciveMessage = array();
        
		$atStart = true;
        $atHeader= true;
		while (!feof($this->fp_handle)) {
    	    $line = fgets($this->fp_handle, 4096);
    	    
    	    if($atStart) {
    	    	$atStart = false;
    	    	if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
    	    		break;
    	    	}
    	    }
    	    if ($atHeader) {
    	        if (trim($line) == '') {
    	            $atHeader = false;
    	            continue;
    	        }
    	        if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
    	            continue;
    	        }
    	        continue;
    	    }
    	    if(strpos($line, 'from') !== false) {
    	    	$from = null; $message = null;
    	    	if(preg_match('@from\s*=\s*([^;]*);?@i', $line, $m)) {
    	    		$from = $m[1];
    	    	}
    	    	if(preg_match('@msg\s*=\s*([^;]*);?@i', $line, $m)) {
    	    		$message = $m[1];
    	    	}
    	    	if(!empty($message)) {
    	    		$reciveMessage[ ] = array($from, $message);
    	    	}
    	    }
        }
        return $reciveMessage;
	}
	
	/**
	* Function for create connection
	*@params:
	*	method: String Request method
	*	file: String Request file
	*	referer: String Referer
	*	cookie: String cookie for send by request
	*	content: String data content to post to server
	*	close: Boolean False for keep connection handle and not close it
	* @access public
	*/
	function _createConnection($method = 'GET', $file = '/', $referer = null,
								$cookie = null, $content = null, $close = true)
	{
		$this->fp_handle = @fsockopen ($this->serverHost, 80, $errno, $errstr, 100);
		if(!$this->fp_handle) {
			exit("can not connect to $this->serverHost");
		}
		
		$method = strtoupper($method);
		if(!in_array($method, array('GET', 'POST')) || is_null($content)) {
			$method = 'GET';
		}
		
		if(is_null($referer)) {
			$referer = $this->serverHost;
		}
		
		if(is_null($cookie)) {
			$cookie = $this->_cookie;
		}
		
		$data = "$method $file HTTP/1.1\n";
		$data.= "Accept: */*\n";
		$data.= "Accept-Language: fa\n";
		$data.= "Referer: $referer/\n";
		$data.= "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\n";
		$data.= "Accept-Encoding: gzip, deflate\n";
		$data.= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Avant Browser [avantbrowser.com]; .NET CLR 1.1.4322)\n";
		$data.= "Host: $this->serverHost\n";
		$data.= "Connection: Keep-Alive\n";
		$data.= "Cache-Control: no-cache\n";
		$data.= "Cookie: $cookie\n";
		
		if($method == 'POST') {
			$length = strlen($content);
			$data.= "Content-Length: $length\n\n";
			$data.= $content;
		} else {
			$data.= "\n";
		}
		
		@fputs ($this->fp_handle, $data);
		if($close) @fclose($this->fp_handle);
	}
}
 
 
?>

Open in new window

0
Comment
Question by:indianbill007
  • 4
  • 3
8 Comments
 
LVL 10

Expert Comment

by:stormist
ID: 24288816
0
 

Author Comment

by:indianbill007
ID: 24288911
Hey stormist,

Thanks for replying. But I already downloaded this class and tried that as my 1st solution, doesn't work!

It too basically does the same thing - connect to e-messenger.net ( which is what eBuddy was formerly known as ) and tries to send messages to IMs using it as the middle man.
0
 
LVL 10

Expert Comment

by:stormist
ID: 24290700
Hi Indianbill,

How about this one?
http://flumpcakes.co.uk/php/msn-messenger
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:stormist
ID: 24290702
PS, I'm sure you know but MSN can connect to the yahoo network as well.
0
 

Author Comment

by:indianbill007
ID: 24294973
Thanks man.

I did know that we can add MSN buddies to Yahoo, but never really gave the 'MSN being able to connect to Yahoo' bit a thought :(

A million thanks though, I will surely try out the above class and see if it helps me in any way.


0
 

Author Comment

by:indianbill007
ID: 24311502
Doesn't work :(

Downloaded a couple more classes from phpclasses, don't work either.

Seems Java has an API which could serve my purpose. Let's see. Any suggestions there?
0
 
LVL 2

Accepted Solution

by:
scriptlancerinbd earned 1050 total points
ID: 24513060
Hi!

You might get some help from another thread of EE. Please look at http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22049606.html.

Regards,

--S
0
 

Author Closing Comment

by:indianbill007
ID: 31577255
There was no accuracy in the solution provided.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question