WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Yahoo! Messenger Friend List
Categories : PHP, CURL, HTML and PHP Click here to Update Your Picture
Ehsan Haque
Date : May 18th 2006
Grade : 4 of 5 (graded 6 times)
Viewed : 23144
File : 4399.zip
Images : No Images for this code example.
Search : More code by Ehsan Haque
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This example shows how to grab your Yahoo! messenger list using PHP cURL

@Author : Ehsan Haque
@Web : http://ehsan.bdwebwork.com
@Demo : http://resource.bdwebwork.com/yahoo_msgr/
@Critical : Update Information to run on localhost: http://ehsan.bdwebwork.com/blog?a=d&b=26&s=4

login.html
<html>
  <head>
    <title>Get your Yahoo! Address Book</title>
  </head>
 
  <body>
    <font face='verdana' color='#999999'>
          <form action="get_list.php" method="post">
              <table width="40%" border="0" align="center" style="font-family:verdana, arial; font-size:12px; border: 1px solid #dddddd">
                  <tr bgcolor="#889933">
                    <td align="center" colspan="2"><b>Your Yahoo! Messenger List</b></td>
                  </tr>
                    <tr>
                        <td align="right">Yahoo! ID:</td>
                        <td><input name="login" id="username" value="" size="17" type="text"></td>
                    </tr>
                    <tr>
                        <td align="right">Password:</td>
                        <td><input name="passwd" id="passwd" value="" size="17" type="password"></td>
                    </tr>
                  <tr>
                    <td align="center" colspan="2"><input type="submit" value="Get My List Now!!" style="font-family:verdana, arial; font-size:12px; background: #FFFFFF"></td>
                  </tr>
                  <tr bgcolor="#889933">
                    <td align="LEFT" colspan="2" style="font-size:10px;color:#fff">
                    AUTHOR: EHSAN HAQUE
                    <br>
                    WEB: <a href="http://ehsan.bdwebwork.com" target="_blank" style="text-decoration:none;color:#fff">HTTP://EHSAN.BDWEBWORK.COM</a>
                    <br>
                    GENERATING Yahoo! Messenger FRIEND LIST USING PHP cURL
                    </td>
                  </tr>
                </table>   
            </form>
      </font>
    </body>
   
</html>


get_list.php

<?

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

// Redirecting user to login page if login information is not provided
if (($login == "") || ($passwd == ""))
  return
header("Location: login.html");

/*
--------------------------------------------------------------------------------
Following part of the script is used to authenticate user on Yahoo!
--------------------------------------------------------------------------------
*/

// Setting URL that is used to authenticate user on Yahoo
$url        = "https://login.yahoo.com/config/login?";

// Setting user agent
$useragent  = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";

// Initiallizing cURL
$ch = curl_init();

/*
Setting cURL options
- User agent
- HTTP Referer
- URL
- Number of input fields to be sent via POST method
- Defining input fields key and value
  - value for the input field .done will change depending on the service page you want to visit
- Cookie file to keep session information
- File where output to be saved
- Specifying not to include header in output
*/
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, "http://mail.yahoo.com");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 22);
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=$login&passwd=$passwd&.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=http://messenger.yahoo.com/edit/");

// Setting encrypted filename to store cookie information
$cookieFilename = $login;
$cookieFilename = MD5($cookieFilename);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilename);       

// Setting filename for the output generated by cURL
$verifyFile = "verifyFile." . MD5($cookieFilename) . ".txt";
$fp = fopen($verifyFile, "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

// Executing cURL
curl_exec($ch);

fclose($fp);

// Reading the content of the output
$verifyFileContent = file_get_contents($verifyFile);

/*
Checking whether Yahoo could authenticate the user
- If not authenticated
  - closes the cURL
  - deletes the output file, cookie file
  - redirects user to login page
- Continues if authenticated
*/
if (preg_match("/invalid/i", $verifyFileContent))
{
 
curl_close ($ch);
 
unlink($verifyFile);
 
unlink($cookieFilename);
  return
header("Location: login.html");
}
else
 
unlink($verifyFile);

curl_error($ch);

curl_close ($ch);

/*
--------------------------------------------------------------------------------
Following part of the script is used to generate output of desired service
- Messenger list in this case
--------------------------------------------------------------------------------
*/

// Setting second URL which provides the messenger list
$url1       = "http://messenger.yahoo.com/edit/";

$useragent1 = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";

$ch1 = curl_init();

curl_setopt($ch1, CURLOPT_USERAGENT, $useragent1);
curl_setopt($ch1, CURLOPT_REFERER, "http://messenger.yahoo.com/");
curl_setopt($ch1, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_POST, 0);

// Specifying the Cookie file to be used to make sure the user is authenticated
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookieFilename);       
curl_setopt($ch1, CURLOPT_COOKIE, 1);

// Setting the filename to save the output generated by cURL
$filename = "list." . MD5($cookieFilename) . ".txt";
$fp = fopen($filename, "w");

curl_setopt($ch1, CURLOPT_FILE, $fp);
curl_setopt($ch1, CURLOPT_HEADER, 0);

curl_exec($ch1);

curl_close ($ch1);

fclose($fp);

// Processing the output generated by cURL
include("process_output.php");

// Delete the cookie file and generated outout file
unlink($cookieFilename);
unlink($filename);

?>


process_output.php

<?php

// Reading the output generated by cURL as an array
$sourceArr          = file($filename);

// Cleaning unnecessary lines from top of the output
$cleanSourceArr     = cleanSource($sourceArr, "top");

// Reversing the output array
$cleanSourceArr     = array_reverse($cleanSourceArr);

// Cleaning the unnecessary lines from the bottom of the output
$cleanSourceArr     = cleanSource($cleanSourceArr, "bottom");

// Setting the array back to its original order
$cleanSourceArr     = array_reverse($cleanSourceArr);

// Removing the texts like [Refresh list], [Send a Message], [Delete]
foreach ($cleanSourceArr as $key => $value)
{
 
$p[0] = "/(\[.[^\]]+)/i";
 
$p[10] = "/\]/";
 
$list[] = preg_replace($p, "", $value);
}

// Removing the HTML tags from the output
$cleanListArr       = cleanHTML($list);

// Seperating array by your messenger's Contact Group
$myListArr          = explode("<b>", $cleanListArr[1]);

foreach (
$myListArr as $key => $value)
{
 
$value = trim($value);
  if (
$value != " ")
  {
   
// Removing 
   
$value          = str_replace(" ", "", $value);
   
   
// Removing </b> tag
   
$value          = str_replace("</b>", "", $value);
   
   
/*
    Creating a child array under each Contact Group array index
    - Each contact is seperated by ·
    */
   
$myList[]       = explode("·", $value);
  } 
}
   
   
// Producing your own output
   
echo "<title>" . str_replace(":", "", strip_tags($cleanListArr[0])) . "</title>";
    echo
"<font face='verdana' size='2' color='#000'>";
    echo
$cleanListArr[0];
    echo
"<table border='1' width='30%' cellpadding='3' cellspacing='2' style='font-size:10px'>";
    echo
"<tr bgcolor='#889933' style='color:#fff;'><td>Friend List</td></tr>";
   
    if (!empty(
$myList))
    {
      foreach (
$myList as $key => $value)
      {
        foreach (
$value as $subKey => $subValue)
        {
          echo
"<tr";
          if (
$subKey === 0)
          {
           
// Highlighting the row if the $subKey refers to a Contact Group name
           
echo " bgcolor='#E09487' style='font-weight:bold;'";
          }         
          echo
"><td>";
          if (
$subKey !== 0)
          {
           
// Displaying the online status of the contact
           
echo "<img src='http://opi.yahoo.com/online?u=$subValue&m=g' border='0'> ";
          }
         
         
/*
          Contact's yahoo ID on your list
            - Removing n
              - n is shown in the output when the contact's Add Request is Pending
          */
         
echo str_replace("n", "", $subValue);
          echo
"</td></tr>";
        } 
      } 
    }

    echo
"</table></font>";

/*
This function is used to clean the HTML tags
- It keeps <b> because it seperates each Contact Group
*/

function cleanHTML($list)
{
  foreach (
$list as $key => $value)
  {
   
$cleanValue = strip_tags($value, "<b>");
   
$cleanValue = trim($cleanValue);
    if (
$cleanValue)
     
$cleanList[] = $cleanValue; // Setting new array without HTML tags (except <b>)
 
}

  return
$cleanList;
}

function
cleanSource($sourceArr, $turn)
{
  foreach (
$sourceArr as $key => $value)
  {
   
// Removing unnecessary lines from top of the output
   
if ($turn == "top")
    {
     
// Stop cleaning at this point
     
if (preg_match("/friend list for/i", $value))
      {
        return
$sourceArr;
      }
      else
      {
       
// Unsetting the array index
       
unset($sourceArr[$key]); 
      } 
    }
   
// Removing unnecessary lines from bottom of the output
   
else if ($turn == "bottom")
    {
     
// Stop cleaning at this point
     
if (preg_match("/\/table/i", $value))
      {
        return
$sourceArr;
      }
      else
      {
        unset(
$sourceArr[$key]); 
      } 
    }   
  }
}

?>



PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
Real simple example of removing HTML tags from text then changing \n (new line) to <br>. Could be used in a forum for instance.
Categories : HTML, PHP, HTML and PHP, Beginner Guides
phpEasyMail: An easy way to send data from HTML-forms via EMail.
Categories : Email, HTML and PHP, Complete Programs, PHP
Vote-Poll script that has a wrapper class that allows the user to create multiple polls on the same page with little trouble.
Categories : PHP, PHP Classes, HTML and PHP
PHP function which gets all the data from a webpage into a string, we can perform regular expression functions on that data afterwards to get our desired data.
Categories : PHP, URLs, HTML and PHP
GonxTabs : Create elegant HTML tabs based interface
Categories : Navigation, HTML, HTML and PHP, PHP
Validator 98 - a PHP-script to generate form-validation-code in JavaScript.
Categories : Complete Programs, Java Script, PHP, HTML and PHP
Complex paging with no resultset limit
Categories : PHP, MySQL, Databases, Output Control, HTML and PHP
How to pass an array from one PHP Script to another via an HTML form
Categories : PHP, HTML and PHP, Arrays
Functions for loading images into a MySQL database and displaying them.
Categories : Graphics, HTML and PHP, MySQL, PHP, Databases
Sql Builder
Categories : PHP, HTML and PHP, Databases, General SQL, Form Processing
phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
Simple script to passing persistent and growing array between recalls of one page (manipulate little stack).
Categories : Arrays, Global Variables, PHP, HTML and PHP, Variables
Website colour changer
Categories : PHP, HTML and PHP, Date Time