所以我使用 xmpphp 库得到了这个 xmpp 头像问题。我写了这个函数来启动类:
/*
* arg 1: $file_handle = the local path to the image
* arg 2: $file_type = the file extension, jpg, png etc.
*/
function upload_new_xmpp_avatar($file_handle, $file_type)
{
// XMPPHP class. Used to connect to xmpp server
require_once "xmpphp/XMPPHP/XMPP.php;
$xmpp_host = "xmpp.domain.com";
$xmpp_server = ""xmpp.domain.com";
$xmpp_port = "5222";
$admin_username = "admin_user";
$admin_pasword = "admin_pw";
$conn = new XMPPHP_XMPP($xmpp_host, $port, $admin_username, $admin_password, "xmpphp", $xmpp_server, $printlog = false, $loglevel = XMPPHP_Log::LEVEL_VERBOSE);
try
{
// load the image from filesystem
$raw_file = file_get_contents($file_handle);
// get the image information array, width, height etc.
$image_info = getimagesize($file_handle);
// build sha1 hash of the raw image data
$image_sha1_hash = sha1($file_handle);
// base64 encode it!
$file_data = base64_encode($raw_file);
$conn->connect();
$conn->processUntil('session_start');
$conn->upload_new_avatar_from_file($user_r->user_name, $file_data, $image_info, $image_sha1_hash);
$conn->disconnect();
}
catch(XMPPHP_Exception $e)
{
api_die("xmpp_error: ".$e->getMessage());
}
return $image_sha1_hash;
}
还需要在 XMPP.php 类中添加一个新的类函数:
// adding upload avatar option
public function upload_new_avatar_from_file($user_name, $file_data, $image_info, $image_sha1_hash) // 08 01 2013 NM adding upload new avatar function
{
$id = 'upload_avatar_file_' . $this->getID();
$image_file_size = ($image_info['bits']*8); // convert bits to bytes.. dur.
$image_mime_type = $image_info['mime'];
$image_height = $image_info[1];
$image_width = $image_info[0];
$xml = "<iq type='set' to='$user_name@$xmpp_server' id='$id'>
<vCard xmlns='vcard-temp'>
<PHOTO xmlns='vcard-temp'>
<BINVAL xmlns='vcard-temp'>$file_data</BINVAL>
<TYPE xmlns='vcard-temp'>$image_mime_type</TYPE>
</PHOTO>
</vCard>
</iq>";
// 2nd xml comand sets the uploaded image as the new vCard avatar
$xml2 = "<presence>
<priority>30</priority>
<status>Online</status>
<x xmlns='vcard-temp:x:update'>
<photo>$image_sha1_hash</photo>
</x>
<x xmlns='jabber:x:avatar'>
<hash>$image_sha1_hash</hash>
</x>
<c xmlns='http://jabber.org/protocol/caps' node='http://vacuum-im.googlecode.com' ver='nvOfScxvX/KRll5e2pqmMEBIls0=' hash='sha-1'/>
</presence>";
// end vacuum vCard example
// http://xmpp.org/extensions/xep-0084.html node and metadata example
$this->addIdHandler($id, 'upload_avatar_handler');
// send the 1st packet
$this->send($xml);
$this->addIdHandler($id2, 'upload_avatar_handler');
// send the 2nd packet
$this->send($xml2);
}
protected function upload_avatar_handler($xml)
{
switch ($xml->attrs['type'])
{
case 'error':
$this->event('upload_new_avatar', 'error');
break;
default:
$this->event('upload_new_avatar', 'default');
break;
}
}
希望它可以帮助尝试做同样的人!