Ehsanul Haque >> Personal Site & Blog

Welcome to my personal site & blog

Yahoo! Services with PHP cURL

Posted on | May 18, 2006 |

Using cURL in PHP is not that tough after all. Just the other day I came up with a script that grabs the Yahoo! Address Book. Today I have modified the same script to grab Yahoo! Messenger friend list.

Check out grabbers here.

One must know the URLs that Yahoo! goes through and data that it passes for authentication. After cURL helps you to authenticate on Yahoo, you simply write a Cookie file to contain the session information. This file is used later part of the script where cURL helps you to grab the address book or messenger list.

You will need to trick Yahoo by sending header information like HTTP Referrer. Address book is grabbed in CSV format, which needs to be parsed to show your desired information. There are 82 columns that address book sends you!! For messenger it is just cleaning up a HTML file generated by cURL. You instruct cURL not to give any output of the generated result, then save it on a temporary file, clean up unnecessary information, reformat the remaining information and print it on the screen. At the end you MUST use unlink() to delete all the files that were generated during the process. Cookie file, messenger list file etc. I have deleted them, being a good boy!!

Here is the partial sample code.

https://login.yahoo.com/config/login?”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 22);
curl_setopt($ch, CURLOPT_POSTFIELDS, …..!!! STOP HERE

The script for grabbing Yahoo messenger list has been published on weberdev.com. Please click “Yahoo! Messenger Friend List” to see my code on weberdev.com and download the ZIP attached to the example.

~~EnJoY~~

Comments

17 Responses to “Yahoo! Services with PHP cURL”

  1. Hasin Hayder
    May 18th, 2006 @ 2:46 pm

    Hi
    That was an amazing task. I have seen these requests several times in many user group. Some even sells their solution instead of show how they do it

    I wish you all the best for such a great job. TC

  2. sunil patekar
    May 18th, 2006 @ 11:32 pm

    Hello Sir,
    This is Sunil patekar.I am a Web Developer.
    i show ur personal web site.I m very impressed.
    i want to import address book of yahoo,gmail.hotmail.
    if u have plz send url script.
    thanks alot.

    nice meet u

  3. nhm tanveer hossain khan (hasan)
    May 19th, 2006 @ 3:05 am

    Gr8 …
    did a gr8 job … becase you made it open…

  4. arun
    May 19th, 2006 @ 3:04 pm

    I downloaded code from your http://www.weberdev.com/get_example-4399.html

    I tested ur code from my localhost. I already enabled curl dll also and i tested some other curl applications it worked well. But ur code throws some warning. It is not working properly. what shall i have to do to make it work properly?

  5. Rupom
    May 19th, 2006 @ 3:57 pm

    Great work you did. Go ahead.

  6. Ehsan Haque
    May 19th, 2006 @ 5:16 pm

    Hi Arun

    Thanks for downloading the code and your comments.

    Basically as cURL has to access https (i.e. secured server) it requires SSL certificate file and you will have to set cURL options with CURLOPT_SSLCERT and CURLOPT_CAINFO.

    I tried to implement SSL connection with cURL but could not do it on localhost. I uploaded my script on to my server and it worked fine. As it has the requries files. I am working over it and hopefully will be able to plug it in very soon.

    Also Thanks to everyone else for their nice comments. Hope to update the code very soon to add more features and solve the problems like the one Arun mentioned.

    Thanks

  7. somnath
    August 18th, 2006 @ 7:22 pm

    I have donloaded your script from http://www.weberdev.com/get_example-4399.html
    but it is not working? I want to import yahoo address book what should I do?

  8. Ehsan Haque
    August 19th, 2006 @ 9:38 pm

    Hi,

    Thank you all for the comments and bug reports. I’ve recieved many queries and have made some update the Grab Yahoo script. Yahoo has also changed some URLs and, therefore, the script might not work.

    I’ve made the Grab Yahoo class which give you the following options to GRAB:

    1. Address Book
    2. Messenger List
    3. Number of Unread Mails in Inbox

    All the services results are returned as an array. Please download the updated version from PHPClasses.org:

    http://www.phpclasses.org/browse/package/3127.html

    Thank you,

    Ehsan

  9. hitesh
    June 4th, 2007 @ 4:42 pm

    hi
    i used ur script for to get yahoo address book,its works to nice. i really impressed.
    i want such script for gmail,hotmail.
    thnx

  10. Usman Khalid
    January 15th, 2008 @ 1:06 pm

    AOA,

    My name is Usman Khalid, i have seen your cURL online ddemo running at http://resource.bdwebwork.com/GrabYahoo/login.html
    and iam very impressed from your work. I never worked in cURL but now i have to as demanded by my company.
    I have read basics n also see grab.yahoo calss file.

    Sir the zip file u have uploaded on phpclasses.net is not working properly, and also it do not have the files you have used in your online demo like login.html, get_list.php etc.

    I am in desprate need of the HARDCORE and outclass work done by you. So kindly can you tell me from where i can get the complete files which give the results just like your online demo.

    Looking forwar to your kind and +ive respose .

    With best regards.

    UsmaN Khalid

  11. Ehsan
    January 15th, 2008 @ 6:33 pm

    Hi,
    You can download the package from http://resource.bdwebwork.com/GrabYahoo/GrabYahoo.zip

    Thanks.

  12. Usman Khalid
    January 16th, 2008 @ 2:50 pm

    AOA,

    isma running this pakade on this url , but is is giving me WARNINGs along with NULL array return.

    http://prototype.evsoft.pk/evs2/webmail/TT/GrabYahoo/login.html

    So kindly can you tell me y iam having this problem.

    Best regards

    Usman khalid

  13. Ehsan
    January 16th, 2008 @ 6:09 pm

    I’ve just tested the link and it filled up the array with my address book content without a problem.

    There are few errors shown which can be fixed by setting proper permission as well as changing few setting on the server.

    Thanks.

  14. Usman Khalid
    January 17th, 2008 @ 7:55 am

    AOA,

    Once again thanks for responding me.Yes i gives list of contacts from my yahoo address book . But it does not gieves me Number of NEW UNREAD EMAILS from the inbox.

    Your Code Demo running return number of UNREAD emails, but my code returns null array for unread mails.

    I am assign task to (1-)list-down all the New Unread Mails. (2-) Read mails (3-)Send Mail for YAHOO, HOTAMIL and GMAIL.

    Currently iam just focusing on YAHOO .

    So what you suggest me?

  15. Ehsan
    January 17th, 2008 @ 9:10 am

    Hi,

    It has been a long time since I last updated the class and as far I was reported by other users that Yahoo Mail may have changed something on the mail page.

    There could be one more reason. If you are using the New Yahoo Mail then my class is (probably) not being able to parse it. When I designed the class the new Ajax based mailbox wasn’t around, therefore, my entire parsing was based on the Classic style mailbox. You may want to switch to your classic mailbox and try to run the class again.

    I’ve just tested my demo. I had the new mailbox enabled on my Yahoo account. When I ran my script on the demo server, it wasn’t giving me any result. As soon as I switched back, it sent back the correct result. So, I bet that is the problem.

    Changing your mailbox display type isn’t the exact fix for the class. There should be some other way. The thing is that, I’ve no time to spare on this class; therefore, cannot really find out if it is a parameter that instructs Yahoo to give classic mailbox or something else.

    Please do some research on it. I will also suggest you take a look at the processAddressbook() which parses the HTML returned by Yahoo mail to find out number of unread mail. Even with the New mailbox, the number of unread mail is shown on TITLE of the page. So you may also want to change the regular expression that handles the parsing and make it look into TITLE tag rather than the tag it is looking into now.

    Hope it helps. Thank you.

  16. Usman Khalid
    January 31st, 2008 @ 3:18 pm

    AOA,

    How r u? thanx 4 ur quick response. I am able to get into the yahoo inbox and alos read all the mails, thanx to you. I infact modify the yahoograb class made by you.
    Once again thanx for guidance.

    Now i hav only one thing to handle. My account is on YAHOO INDIA. now when i got login on mail.yahoo.com. After login the URL bcome http://www.in.f84/ym/…..

    Now “84″ number is only associated to my mail address. It will change if other user from yahoo india trys to login his/her yahoo number after “in.f” will changes.

    Similarly, if somebody from USA Yahoo trys to login , URL will look like “us.f[number]“.

    Now my problem is iam unable to get that URL that is built on runn time . Because of which i can only see my mails. If some how i get that URL like “in.f768″ which is generated on run time, I will be able to read all the inboxes on yahoo.

    So kindly help me out from this. As this projects is getting on my nerves.

    Looking forward for ur kind and +ive response.

    Best Regards

    Muhammad Usman Khalid

  17. Ehsan
    February 6th, 2008 @ 7:04 pm

    Grab Yahoo class’s Messenger List feature was not functioning for several months. With the help of “Traian” on PHPClasses.org, I was able to resolve the issue. The package has been updated on the site and ready for download.

    @Usman: I don’t have any instant solution to your requirement. You may want to check which URL Yahoo redirects to after logging in and parse that to use as the value for $this->serviceUrl.

Leave a Reply





Enter this code