Herewith my uncleaned working code to post message to user:
<?php
$usr = "user";
$pwd = "password";
$host = "chat.lintel.co.za";
$message = "Hallo There Tammy!!\nWhat is news?";
$to = "sales@chat.lintel.co.za";
// Just console logging code (Debugging)
function console_log( $data ){
echo '<script>';
echo 'console.log('. json_encode( $data ) .')';
echo '</script>';
}
// Receive stream id from login and send message
function xmpp_send($host, $streamid, $message, $to, $usr, $pwd){
console_log($streamid);
$payload = Array(
"body" => $message
);
$payload = json_encode($payload);
// Prepare new cURL resource
$ch = curl_init('http://'.$host.':7070/rest/api/restapi/v1/chat/'.$streamid.'/messages/'.$to);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: ' . strlen($payload),
'Authorization: Basic '. base64_encode($usr.':'.$pwd))
);
// Submit the POST request
$result = curl_exec($ch);
// Close cURL session handle
curl_close($ch);
}
// Login with username and password to Openfire.
function xmpp_login($host, $usr, $pwd, $message, $to){
// Prepare new cURL resource
$ch = curl_init('http://'.$host.':7070/rest/api/restapi/v1/chat/'.$usr.'/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 0',
'Authorization: Basic '. base64_encode($usr.':'.$pwd))
);
// Submit the POST request
$result = curl_exec($ch);
xmpp_send($host, $result, $message, $to, $usr, $pwd);
}
xmpp_login($host, $usr, $pwd, $message, $to);
?>
Anyone welcome to use or even improve to make a fully fuctional PHP rest api backend.
Thank You
Albertus Geyser