How to use JAXL (Jabber XMPP Library in PHP) to import Gtalk contacts of any user

JAXL is an open source Jabber XMPP Client library written in PHP. It provides a self titled class JAXL which implements XMPP protocol. It can be extended to write custom event handler for every message or presence received. Developers are using JAXL for developing real time applications. Checkout 5 exciting gaming bots you can make using JAXL.

However one thing which goes un-noticed is that JAXL can also be used to import Gtalk contacts of any user. This is infact one of the very first thing which JAXL class do, after successful authentication with the Gtalk servers i.e. import the authenticated user contact list. In this blog post I will demo a sample script to import any user contact list from google servers.

Importing Gtalk contacts using JAXL

  1. Download and extract jaxl-1.0.4.rar
  2. Edit config.ini.php and update credentials of the user whose contact list we are trying to import:
      $key = array("prod"=>array("user"=>"mailsforabhinav",
                                 "pass"=>"xxxxxx",
                                 "host"=>"talk.google.com",
                                 "port"=>5222,
                                 "domain"=>"gmail.com"
                                ),
    
  3. Open jaxl.class.php and modify the code as below:
        function setStatus() {
          // Set a custom status or use $this->status
          $this->sendStatus($this->status);
          print "Setting Status...n";
          print_r($this->rosterlist); // Print the contact list on the console
          print "Donen";
          exit;
        }
    
  4. Finally run from command line to retrieve gtalk contacts of the authenticated user.
    php index.php

One can easily modify the above code to save user contacts in a database.
Also one can echo json_encode($this->rosterlist) in response to an Ajax call from the browser.

Enjoy and leave your comments.

  • Pankaj

    Hi Abhi,
    Thanks for sharing your work with us but i have one question related to this.
    Its related to point no 2.
    Edit config.ini.php and update credentials of the user whose contact list we are trying to import:
    How can we get the credentials of other users?
    For e.g. i have a gmail friend whose id is abc@gmail.com
    I want to import gtalk contacts of abc using jaxl how can i provide password for abc in config.ini file.
    Please guide me on this.
    Thanks,
    Pankaj

    • Hi Pankaj,

      I guess you are looking for something like this:

      1. A user enters his credentials for importing the friends.
      2. You pass the credentials to Jaxl library.
      3. You get back the response from library in form of json data.
      4. Save/Display them as per your requirement.

      Regarding how to pass user credentials to Jaxl, you will need to tweak the code a little bit. e.g. You can call the script using system command and pass arguments. Inside the script you read them as $argv[1], $argv[2], etc.

      Hope this helps.
      Abhinav

  • sameer

    Hi Abhinav,
    I repeated the same steps as you suggested but I am getting following error

    PHP Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51

    Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51
    PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52
    PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53
    PHP Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51

    Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51
    PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52
    PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53
    PHP Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51

    Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51
    PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52
    PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53
    PHP Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51

    Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51
    PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52
    PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53
    Starting TLS Encryption…
    PHP Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:wampwwwjaxlxmpp.class.php on line 348

    Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto in C:wampwwwjaxlxmpp.class.php on line 348
    PHP Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51

    Warning: fopen(log/logger.log): failed to open stream: No such file or directory in C:wampwwwjaxllogger.class.php on line 51
    PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 52
    PHP Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:wampwwwjaxllogger.class.php on line 53

    • Alright, fix this by replacing log/logger.log by /full/path/to/log/logger.log. That should fix your problem.

      Also SSL error is seen meaning either it is not enable or is not configured properly. Read this for proper some previous discussion on this issue: http://code.google.com/p/jaxl/issues/detail?id=7

    • sameer

      Hi Abhi,
      Thanks for replying.
      giving the full path to logger solved the one problem. But still I am getting the SSL error.

      I tried both solution on that thread
      i.e
      stream_socket_enable_crypto($this->stream, TRUE,
      STREAM_CRYPTO_METHOD_SSLv3_CLIENT);

      or
      if (!stream_socket_enable_crypto($this->stream, TRUE,
      STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
      stream_socket_enable_crypto($this->stream, TRUE,
      STREAM_CRYPTO_METHOD_SSLv3_CLIENT);
      }

      but still smae result.

      Starting TLS Encryption…
      Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:wampwwwjaxlxmpp.class.php on line 349

      Fatal error: Maximum execution time of 60 seconds exceeded in C:wampwwwjaxlxmpp.class.php on line 162

      I am using wamp and using the default configuration for php and apache. Do you have any idea how to fix it?
      THanks in advance

    • Try this from command line:
      php -i | grep openssl

      Or since you are on windows, see the output of phpinfo() command and check if you have openssl enabled with php. If not do it and problem should get fixed. Hopefully 😀

    • sameer

      Hi,
      Thanks
      I looked at the output of phpinfo() and I cna see
      OpenSSL support disabled (install ext/openssl).

      I am using wamp. I checked the ext directory in php folder of wamp but I could ifnd any dll which is similar to openssl. Do u know how to enable it? I un commented extension=php_openssl.dll line in php.ini but it didn’t work. DO I need to install anything?

      by the way is it necessary to have openssl enable to communicated with gtalk?

    • Go to php.net and download the required dll for your version of PHP and enable it.

  • clif

    I am getting the SSL error.

    phpinfo ()
    opensslOpenSSL support enabled
    OpenSSL Version OpenSSL 0.9.8a 11 Oct 2005

    Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto
    in D:jabberSGBotxmpp.class.php on line 348

    [xmpp.class.php][348]
    Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto
    in D:jabberSGBotxmpp.class.php on line 348

    plz help

    • Kindly read my replies to sameer above. You seem to be missing the same things.

    • clif

      OpenSSL support enabled

      My WEB Server supported SSL protocol

      but exist that ERROR

    • php -i | grep openssl

      Run this from command line and see if you have openssl enabled for cli. Enabling for your webserver will not make jaxl work 😀

    • clif

      I have this web-server TopServer 2.1
      In this package exists 3 php*.exe )
      php.exe – main console
      php-win.exe
      php-cgi.exe

      then i run *php -i | grep openssl* in php*.exe, result are anything

    • for me its not working somehow.
      its not able to connect to gtalk

      Trying to connect at talk.google.com:5222

      2009-12-29 09:56:51
      Failed to establish a connection at talk.google.com:5222

    • @Clif,
      I have no experience with TopServer. Nevertheless, running jaxl only requires a PHP installation and no web server. I hope you are trying to run jaxl bot through command line.

      @samir
      What env are you on? Have you updated the config file with your gtalk username and passwords?

    • I have tested Jaxl on various platform including windows, mac, ubuntu, debian, freebsd and RHEL. If you have your env right, it SHOULD work 😀

      Probably checkout closed and known issues here http://code.google.com/p/jaxl/issues/list in case you missed anyone of them.

    • clif

      But my command console hes output:

      D:jabberSGBot>T:usrlocalPHPphp.exe D:jabberSGBotindex.php
      Included module bash.mod.php
      Included module feedback.mod.php
      Included module md5.mod.php
      Starting TLS Encryption…

      Warning: stream_socket_enable_crypto(): this stream does not support SSL/crypto
      in D:jabberSGBotxmpp.class.php on line 348

      don’t like that http://jaxl.googlecode.com/issues/attachment?aid=-4264766588276847936&name=jabb.png&inline=1

    • Hey clif, just curious any luck while working with jaxl, did it worked for you?

  • Kedar Langade

    Hi Abhinav,

    Thank you very much, sharing your knowledge. I learn new things from this article.

  • jpod

    Hi abhinav,

    thanks for great job…!! i have enable my ssl but still got error like this:

    PHP Warning: stream_socket_enable_crypto(): this stream does not support SSL/cr
    ypto in D:htmljaxlxmpp.class.php on line 348

    • Check the know issue list. You don’t have openssl module enabled for PHP CLI.

  • Hi Abhinav,

    I’ve been having similar issues to what some others have reported
    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53
    Starting TLS Encryption…

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53
    Attempting PLAIN Authentication…

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53

    Warning: fopen(C:
    mpphtdocsjaxlloglogger.log): failed to open stream: Invalid argument in C:x
    ampphtdocsjaxllogger.class.php on line 51

    Warning: fwrite() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 52

    Warning: fclose() expects parameter 1 to be resource, boolean given in C:xampp
    htdocsjaxllogger.class.php on line 53
    Starting Session…
    Requesting Feature List…
    Requesting Roster List…
    Done

    I have done everything you said (setting user and pass in config.ini, changing the path of the logger.log file to an absolute path etc.). Do you know what could be the cause of the above error? Thanks

  • Hi Jayraj,

    I would suggest you to write a small script which opens the log file and write some data into it. See if it works for you. Can be a file permission issue also.

    Otherwise you can also disable logging in the config file to remove these warnings.

  • Hi Abhinav,

    Thanks for the quick reply. I have disabled logging as well (

    // Log Level for logger class
    $logEnable = FALSE;

    )in the config.ini.php file. This is how you do it right?

  • rajat

    dude u r a genius

  • subashbabu

    Hai Abhi i have to ask you one doubt. i need that how to get my friends gtalk all contacts with their status and their all information using php&xmpp .please tell me

    • Hi Siva, Checkout the latest version of Jaxl library from github. Latest version of library provide $jaxl->roster variable which will give you list of online friends with their statuses. Try out and lemme know if you face any difficulty. (Hint: Try echobot application and dump $jaxl->roster variable for details)

  • SubashBabu

    Thanks a lot abhi.but i am new of gtalk application . i don’t know how to start to develop this application.so please tell me how to start to develop gtalk application to get all friend’s contacts and their status and to send messages as well as receive messages from them.please abhi it’s very urgent.Thank you so much

    • Hi Subash, This is something i built when i started learning xmpp. You can find all these things happening inside echobot.php (it retrieves friend contact, statuses and even echo back incoming messages). Also do checkout $jaxl->roster variable, which is a local cache of connected xmpp client’s roster.

  • subashbabu

    if i run that the errors are coming like this

    Warning: touch() [function.touch]: Unable to create file /var/log/jaxl.log because Permission denied in /home/youmint/MyYoumint.com/Code/abi/core/jaxl.class.php on line 678

    Fatal error: Uncaught exception ‘JAXLException’ with message ‘Log file /var/log/jaxl.log doesn’t exists’ in /home/youmint/MyYoumint.com/Code/abi/core/jaxl.class.php:678 Stack trace: #0 /home/youmint/MyYoumint.com/Code/abi/app/echobot.php(23): JAXL->__construct(Array) #1 {main} thrown in /home/youmint/MyYoumint.com/Code/abi/core/jaxl.class.php on line 678

    • So simply create the log file in the desired location or customize log path using Jaxl constructor. Read http://jaxl.net for more info.

  • Rajesh

    Hai! I want to learn about jaxl .so which tutorial is best to learn

  • Rajesh

    Hai! how to work this class please explain me.

  • Rajesh

    Hai i want to create gtalk application.so what to do

    • Hi Rajesh,

      You need to checkout Jaxl v 2.x. This example was written using Jaxl v 1.x and will not work directly with 2.x library. Download latest library version and read Jaxl documentation. Have a look at sample applications which are inside /app folder of the downloaded library.

  • Babu

    Hai Abhi . I want to get User MAC Address in PHP. i have searching in google tillnow i could not get it.but i have some code for that but i could not run in server.It’s only working in localserver.if you give me I will import on my website.Thanking You

  • Babu

    Hai Abhi.shall i use gtalk application in my website same as GTalk.is there any change?

  • Rey

    Hi Abhi, how can i get the list of the contacts with JAXL 2.0??
    tnks!

    • Hi Rey,

      After successful authentication and setStatus call you can access entire roster list of connected user by calling $jaxl->roster inside your application code

  • Hello, I am developing on wamp server, is working fine. But when i am hosting it to my linux based remote server it says Error 500 near line $conn=new XMPP_PHP(…), What is problem?

  • Announcing Jaxl v3.x – asynchronous, non-blocking I/O, event based PHP client/server library – http://abhinavsingh.com/blog/2012/07/announcing-jaxl-v3-x-asynchronous-non-blocking-io-event-based-php-clientserver-library/