|
|
|
GrabYahoo - Yahoo Service Grabber class
Version 1.0, Created 05/22/2006
This class is used to grab Yahoo services like
Address Book, Messenger List and return the list in array
Copyright (C) 2006 Ehsan Haque
License: GPL
Example: http://resource.bdwebwork.com/GrabYahoo/
class.GrabYahoo.php
| class GrabYahoo
{
/*-------------------------------------------------------
Public Variables
-------------------------------------------------------*/
/**
* Service name (1. addressbook, 2. messenger)
* @public
* @var string
*/
var $service = "";
/**
* Yahoo! Account Username
* @public
* @var string
*/
var $login = "";
/**
* Yahoo! Account Password
* @public
* @var string
*/
var $password = "";
/**
* Abosolute path to save the cookie
* Default value is DOCUMENT_ROOT
* @public
* @var string
*/
var $cookieJarPath = "";
/**
* Turns the localhost mode On or Off
* Default value is false
* @public
* @var boolean
*/
var $isLocalhost = false;
/**
* Abosulte path to the CA Bundle file
* SSL Certificate required to verify CA cert
* Usually required when script ran on Localhost
* Remote servers may not require
* Default value is false
* @public
* @var string
*/
var $caBundleFile = "";
/**
* Specifies if Proxy server required as Gateaway
* Default value is false
* @public
* @var boolean
*/
var $isUsingProxy = false;
/**
* Proxy host name
* @public
* @var string
*/
var $proxyHost = "";
/**
* Proxy port number
* @public
* @var int
*/
var $proxyPort = 0;
/*-------------------------------------------------------
Private Variables
-------------------------------------------------------*/
/**
* URL to Authenticate user on Yahoo!
* @private
* @var string
*/
var $authUrl = "https://login.yahoo.com/config/login?";
/**
* URL for the desired Service
* @private
* @var string
*/
var $serviceUrl = "";
/**
* URL to be used by cURL
* @private
* @var string
*/
var $url = "";
/**
* User agent (used to trick Yahoo!)
* @private
* @var string
*/
var $userAgent = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";
/**
* Referer URL (used to trick Yahoo!)
* @private
* @var string
*/
var $referer = "http://my.yahoo.com";
/**
* Specifies whether output includes the header
* @private
* @var int
*/
var $showHeader = 0;
/**
* Specifies number of post fields to pass
* @private
* @var int
*/
var $numPostField = 0;
/**
* Specify fields to send via POST method as key=value
* @private
* @var string
*/
var $postFields = "";
/**
* File where output is temporarily saved during authentication
* @private
* @var string
*/
var $authOutputFile = "";
/**
* File where service output is temporarily saved
* @private
* @var string
*/
var $outputFile = "";
/**
* File where Cookie is temporarily saved
* @private
* @var string
*/
var $cookieFileJar = "";
/**
* Cookie File that is read by service process
* This carries same value as $cookieFileJar
* @private
* @var string
*/
var $cookieFile = "";
/**
* Specifies if Cookie is to be in header
* @private
* @var int
*/
var $cookie = 0;
/**
* Proxy address as proxy.host:port
* @private
* @var string
*/
var $proxy = "";
/**
* Sets Service URL
* @return void
*/
function setServiceUrl()
{
if (empty($this->service))
{
$this->setError("provide_service");
return false;
}
// Sets the URL depending on the choosen service
switch ($this->service)
{
case 'addressbook' : $this->serviceUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv"; break;
case 'messenger' : $this->serviceUrl = "http://messenger.yahoo.com/edit/"; break;
}
}
/**
* Sets the Cookie Jar File where Cookie is temporarily saved
* @return void
*/
function setCookieJar()
{
// Sets the encrypted cookie filename using Yahoo! account username
$this->cookieFilename = MD5($this->login);
// Sets the Cookie Jar filename with an absolute path
$this->cookieFileJar = (!empty($this->cookieJarPath)) ? $this->cookieJarPath . "/" . $this->cookieFilename : $_SERVER['DOCUMENT_ROOT'] . "/" . $this->cookieFilename;
}
/**
* Initializes cURL session
* @return void
*/
function initCurl()
{
$this->curlSession = curl_init();
}
/**
* Sets cURL options
* @return boolean
*/
function setCurlOption()
{
// Sets the User Agent
curl_setopt($this->curlSession, CURLOPT_USERAGENT, $this->userAgent);
// Sets the HTTP Referer
curl_setopt($this->curlSession, CURLOPT_REFERER, $this->referer);
// Sets the URL that PHP will fetch using cURL
curl_setopt($this->curlSession, CURLOPT_URL, $this->url);
// Sets the number of fields to be passed via HTTP POST
curl_setopt($this->curlSession, CURLOPT_POST, $this->numPostField);
// Sets the fields to be sent via HTTP POST as key=value
curl_setopt($this->curlSession, CURLOPT_POSTFIELDS, $this->postFields);
// Sets the filename where cookie information will be saved
curl_setopt($this->curlSession, CURLOPT_COOKIEJAR, $this->cookieFileJar);
// Sets the filename where cookie information will be looked up
curl_setopt($this->curlSession, CURLOPT_COOKIEFILE, $this->cookieFile);
// Sets the option to set Cookie into HTTP header
curl_setopt($this->curlSession, CURLOPT_COOKIE, $this->cookie);
// Checking if the script is ran on Localhost or Remote server
if ($this->isLocalhost)
{
if (empty($this->caBundleFile))
{
$this->setError("provide_ca");
return false;
}
// Sets the CA Bundle file for SSL Certificate to verify
// This file is mainly used when the script is ran on Localhost
curl_setopt($this->curlSession, CURLOPT_CAINFO, $this->caBundleFile);
}
// Sets the proxy server as proxy.host:port
curl_setopt($this->curlSession, CURLOPT_PROXY, $this->proxy);
// Sets the filename where output will be temporarily saved
curl_setopt($this->curlSession, CURLOPT_FILE, $this->fileHandler);
// Sets the option to include HTTP header into the output
curl_setopt($this->curlSession, CURLOPT_HEADER, $this->showHeader);
return true;
}
/**
* Executes the Service
* @param string $login Username of user's Yahoo! Account
* @param string $password Password of the user's Yahoo! Account
* @return array|false
*/
function execService($login, $password)
{
$login = trim($login);
$password = trim($password);
if (empty($login))
{
$this->setError("provide_login");
return false;
}
if (empty($password))
{
$this->setError("provide_pass");
return false;
}
$this->login = $login;
$this->password = $password;
$this->setServiceUrl();
// Instructs to authenticate user on Yahoo!
$this->auth = $this->doAuthentication();
if ($this->auth)
{
// Instructs to fetch output if Authenticated
$this->getServiceOutput();
return $this->serviceOutput;
}
}
/**
* Authenticates user on Yahoo!
* @return boolean
*/
function doAuthentication()
{
// Instructs to initialize cURL session
$this->initCurl();
// Sets the URL for authentication purpose
$this->url = $this->authUrl;
// Sets the number of fields to send via HTTP POST
$this->numPostField = 22;
// Sets the fields to be sent via HTTP POST as key=value
$this->postFields = "login=$this->login&passwd=$this->password&.src=&.tries=5&.bypass=&.partner=&.md5=&.hash=&.intl=us&.tries=1&.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO&.u=dmvmk8p231bpr&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.v=0&.chkP=N&.last=&.done=" . $this->serviceUrl;
// Instructs to set Cookie Jar
$this->setCookieJar();
// Sets the filename for output generated during authentication
$this->authOutputFile = "auth.output." . MD5($this->login) . ".txt";
// Instructs to open a file for writing the output
$this->fileHandler = fopen($this->authOutputFile, "w");
// Checks if the cURL options are all set properly
if ($this->setCurlOption())
{
// Instructs to execute cURL session
$this->execCurl();
// Checks if any cURL error is generated
if ($this->getCurlError())
{
$this->setError("curl_error");
return false;
}
fclose($this->fileHandler);
// Sets the content of output as a string
$authContent = file_get_contents($this->authOutputFile);
// Checks if the authentication failed, either invalid login or username is not registered
if ((preg_match("/invalid/i", $authContent)) || (preg_match("/not yet taken/i", $authContent)))
{
// Instructs to close cURL session
$this->closeCurl();
// Unlinks the output file
$this->unlinkFile($this->authOutputFile);
// Unlinks the cookie file
$this->unlinkFile($this->cookieFileJar);
$this->setError("invalid_login");
return false;
}
$this->unlinkFile($this->authOutputFile);
$this->closeCurl();
}
return true;
}
/**
* Sets the Service Output
* @return void
*/
function getServiceOutput()
{
// Checks if the user needs proxy (to be set by user)
if ($this->isUsingProxy)
{
// Checks if the proxy host and port is specified
if ((empty($this->proxyHost)) || (empty($this->proxyPort)))
{
$this->setError("proxy_required");
$this->unlinkFile($this->cookieFileJar);
return false;
}
// Sets the proxy address as proxy.host:port
$this->proxy = $this->proxyHost . ":" . $this->proxyPort;
}
// Instructs to process the choosen service
switch ($this->service)
{
case 'addressbook' : $this->showHeader = 0;
$this->serviceOutput = $this->processAddressBook();
break;
case 'messenger' : $this->showHeader = 0;
$this->serviceOutput = $this->processMessengerList();
break;
}
$this->unlinkFile($this->cookieFileJar);
}
/**
* Processes Yahoo! Address Book
* @return array|false
*/
function processAddressBook()
{
$this->initCurl();
$this->url = $this->serviceUrl;
$this->numPostField = 1;
$this->postFields = "Outlook=Export Now";
$this->cookieFile = $this->cookieFileJar;
$this->outputFile = "addressBook." . md5($this->login) . ".txt";
$this->fileHandler = fopen($this->outputFile, "w");
if ($this->setCurlOption())
{
$this->execCurl();
$this->closeCurl();
fclose($this->fileHandler);
// Sets the service output as an array
$fileContentArr = file($this->outputFile);
// Sets the address book column headings
$abColumnHeadLine = trim($fileContentArr[0]);
$abColumnHeadLine = str_replace("\"", "", $abColumnHeadLine);
// Sets the address book column headings into an array
$abColumnHeadArr = explode(",", $abColumnHeadLine);
// Unsets the heading line from the file content array
unset($fileContentArr[0]);
foreach ($fileContentArr as $key => $value)
{
// Sets the address book list individually
$listColumnLine = trim($value);
$listColumnLine = str_replace("\"", "", $listColumnLine);
// Sets the individual list into an array
$listColumnArr = explode(",", $listColumnLine);
// Iterates through each item of individual address in the list
foreach ($listColumnArr as $listColumnKey => $listColumnValue)
{
// Sets the column heading as key
$listKey = $abColumnHeadArr[$listColumnKey];
// Sets the value for the key respectively
$list_[$listKey] = $listColumnValue;
}
// Sets the address book list in an array
$list[] = $list_;
}
$this->unlinkFile($this->outputFile);
return $list;
}
}
/**
* Processes Yahoo! Messenger Friend List (Grouped)
* @return array|false
*/
function processMessengerList()
{
$this->initCurl();
$this->url = $this->serviceUrl;
$this->cookieFile = $this->cookieFileJar;
$this->outputFile = "messengerList." . md5($this->login) . ".txt";
$this->fileHandler = fopen($this->outputFile, "w");
if ($this->setCurlOption())
{
$this->execCurl();
$this->closeCurl();
fclose($this->fileHandler);
// Sets the service output as an array
$fileContentArr = file($this->outputFile);
foreach ($fileContentArr as $key => $value)
{
$value = trim($value);
// Clears all the HTML tags except <b> and <a>
$value = strip_tags($value, "<b><a>");
// Sets the pattern for regular expression replacement
$pattern[0] = "/(\[.[^\]]+)/i";
$pattern[10] = "/\]/";
// Replaces anything matching [anyString]
$trimmedContent[] = preg_replace($pattern, "", $value);
}
foreach ($trimmedContent as $key => $value)
{
// Finds only the array items containing pm_friend
if (preg_match("/pm_friend/i", $value))
{
// Clears all <a> tags
$listArr[] = strip_tags($value, "<b>");
}
}
foreach ($listArr as $key => $value)
{
$value = str_replace(" ", "", $value);
// Sets the array seperating the Messenger Contact Groups
$listGroupArr = explode("<b>", $value);
}
foreach ($listGroupArr as $key => $value)
{
if (!empty($value))
{
$value = str_replace("</b>", "", $value);
// Sets the array with contacts seperated in different array index
$listArrForGroup[] = explode("·", $value);
}
}
foreach ($listArrForGroup as $key => $value)
{
foreach ($value as $subKey => $subValue)
{
if ($subKey === 0)
{
// Sets the Contact Group name as the array key
$arrKey = trim($subValue);
}
if ($subKey !== 0)
{
// Sets the array of contacts by the Contact Group
// preg_replace is used to replace any non alphanumeric character or an underscore
$subValue = trim($subValue);
$subValue = preg_replace("/\Wn/", "", $subValue);
$list[$arrKey][] = $subValue;
}
}
}
$this->unlinkFile($this->outputFile);
return $list;
}
}
/**
* Executes cURL Session
* @return void
*/
function execCurl()
{
curl_exec($this->curlSession);
}
/**
* Closes cURL session
* @return void
*/
function closeCurl()
{
curl_close($this->curlSession);
}
/**
* Unlinks any given file
* @return void
*/
function unlinkFile($fileToUnlink)
{
if (file_exists($fileToUnlink))
{
unlink($fileToUnlink);
}
}
/**
* Sets any cURL error generated
* @return boolean
*/
function getCurlError()
{
$this->curlError = curl_error($this->curlSession);
return (!empty($this->curlError)) ? true : false;
}
/**
* Sets Error Information
* @return void
*/
function setError($error)
{
$msg = (!empty($error)) ? $this->getErrorInfo($error) : null;
$this->errorCount++;
$this->errorInfo = $msg;
}
/**
* Provides the Error message
* @param string $error Error code for which error message is generated
* @return string
*/
function getErrorInfo($error)
{
switch ($error)
{
case 'provide_service' : $msg = "Must specify a Service"; break;
case 'provide_login' : $msg = "Must provide Login name"; break;
case 'provide_pass' : $msg = "Must provide Password"; break;
case 'provide_ca' : $msg = "Must provide a SSL Certificate to verfiy CA cert"; break;
case 'proxy_required' : $msg = "Must provide both Proxy host and port"; break;
case 'invalid_login' : $msg = "Login information incorrect"; break;
case 'curl_error' : $msg = $this->curlError; break;
}
return $msg;
}
}
?> | |
Usage Example
| <?
require_once('class.GrabYahoo.php');
// Yahoo! Account Username
$login = "username";
// Yahoo! Account Password
$password = "password";
// Initializing Class
$yahoo = new GrabYahoo;
/*
Setting the desired Service
1. addressbook => Used to grab Yahoo! Address Book
2. messenger => Used to grab Yahoo! Messenger List
*/
$yahoo->service = "addressbook";
// The CA Bundle file, used to run the script on Localhost
$yahoo->caBundleFile = $_SERVER['DOCUMENT_ROOT'] . "/path/ca-bundle.crt";
/*
Set to true if running on Localhost
- Setting it to true will require a CA Bundle file
*/
$yahoo->isLocalhost = true;
/*
Set to true if HTTP proxy is required to browse net
- Setting it to true will require to provide Proxy Host Name and Port number
*/
$yahoo->isUsingProxy = true;
// Set the Proxy Host Name
$yahoo->proxyHost = "proxy.host";
// Set the Proxy Port Number
$yahoo->proxyPort = "port";
// Set the location to save the Cookie Jar File
$yahoo->cookieJarPath = $_SERVER['DOCUMENT_ROOT'] . "/path/";
/*
Execute the Service
- Require to pass Yahoo! Account Username and Password
*/
$yahooList = $yahoo->execService($login, $password);
// Printing the array generated by the Class
dump($yahooList);
// Use this line to figure out any error generated during the process
echo $yahoo->errorInfo;
function dump($var)
{
echo "<pre>";
print_r($var);
echo "</pre>";
}
?> | | |
|
| These PHP Classes Check if a host is alive using various methods. Categories : PHP, PHP Classes, Sockets, CURL | | | Grab Gmail Addressbook Categories : PHP, PHP Classes, CURL | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | Url To Pdf Report By Remote Application Categories : PHP, PHP Classes, PDF, CURL | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | Import the yahoo address book. Categories : PHP, CURL, Authentication | | | an example of the cyberlib payment class Categories : PHP, PHP Classes, Ecommerce, Credit Cards | | | Power Form Validation Categories : PHP, PHP Classes, Data Validation | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | |
|
|
|