xmpphp XMPP, send message from php script

2019-07-16 12:24发布

问题:

Hi I have a jabberserver and i would like to be able to push messages out to users from a php script.

F.x. if i call script.php from my browser, it sends a message to a user. I've tried both with jaxl and xmpphp which are xmp frameworks, but i cant get it to work. Not with my own server, and neither with facebooks server.

I have the following ind my script.php:

error_reporting(E_ALL);    
include("lib/xmpphp/XMPPHP.php");  
$conn = new XMPP('chat.facebook.dk', 5222, 'username', 'password', '', 'chat.facebook.com', true, XMPPHP_Log::LEVEL_VERBOSE);  
$conn->connect();  
$conn->processUntil('session_start');  
$conn->message('someusername@chat.facebook.com', 'This is a test message!');  
$conn->disconnect();    

But nothing happends and no errors either. I've followed this guide to set up an echobot, and it works with both my server and facebook. The script is here: http://abhinavsingh.com/blog/2010/02/writing-your-first-facebook-chat-bot-in-php-using-jaxl-library/ <-- and is run on the server commandline, and waiting for a message, and then replys.

What do i have to do ?

回答1:

<?php 
set_time_limit(0);  // some time connection take while  
require_once 'xmpp-lib/XMPPHP/XMPP.php';  
$host = 'you Host name'; // ex.192.168.2.1  
$port = '5222'; // its defauls xmpp port 
$username = 'name@host' // ex vivek@host 
$pass = 'userpass';  
$conn = new XMPPHP_XMPP(host , $port, $username, $pass, 'xmpphp','yourhost', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);  
try {
         $conn->connect();
         $conn->processUntil('session_start');
         $conn->presence();
         $conn->message('anotherusername@host', 'Hello!');
         $conn->disconnect(); 
} catch(XMPPHP_Exception $e) {
         die($e->getMessage()); 
} 
?>