• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

How to send PMs to Yahoo ids using PHP/CURL

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
indianbill007
Asked:
indianbill007
  • 4
  • 3
1 Solution
 
indianbill007Author Commented:
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
 
stormistCommented:
Hi Indianbill,

How about this one?
http://flumpcakes.co.uk/php/msn-messenger
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
stormistCommented:
PS, I'm sure you know but MSN can connect to the yahoo network as well.
0
 
indianbill007Author Commented:
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
 
indianbill007Author Commented:
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
 
scriptlancerinbdCommented:
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
 
indianbill007Author Commented:
There was no accuracy in the solution provided.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now