Huanxin instant messaging

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn about huanxin instant messaging.
Address: http://www.easemob.com/can make a chat system
According to the interface documentation, I sorted it out! Share
Class Hxcall
{


Private $ app_key = 'qqqqqq # aaaaa ';

Private $ client_id = 'sdasdasd ';

Private $ client_secret = 'sdsdsdsd ';

Private $ url = "https://a1.easemob.com/qqqqqq/aaaaaa ";
/*
* Get the APP administrator Token
*/
Function _ construct ()
{
$ Url = $ this-> url. "/token ";
$ Data = array (
'Grant _ type' => 'client _ credentials ',
'Client _ id' => $ this-> client_id,
'Client _ secret' => $ this-> client_secret
);
$ Rs = json_decode ($ this-> curl ($ url, $ data), true );
$ This-> token = $ rs ['Access _ token'];
}
/*
* Registering an IM user (authorized registration)
*/
Public function hx_register ($ username, $ password, $ nickname)
{
$ Url = $ this-> url. "/users ";
$ Data = array (
'Username' => $ username,
'Password' => $ password,
'Nickname' => $ nickname
);
$ Header = array (
'Content-Type: application/json ',
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, $ data, $ header, "POST ");
}
/*
* Add a friend to an IM user
*/
Public function hx_contacts ($ owner_username, $ friend_username)
{
$ Url = $ this-> url. "/users/$ {owner_username}/contacts/users/$ {friend_username }";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "POST ");
}
/*
* Revoking the friend relationship of an IM user
*/
Public function hx_contacts_delete ($ owner_username, $ friend_username)
{
$ Url = $ this-> url. "/users/$ {owner_username}/contacts/users/$ {friend_username }";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "DELETE ");
}
/*
* View friends
*/
Public function hx_contacts_user ($ owner_username)
{
$ Url = $ this-> url. "/users/$ {owner_username}/contacts/users ";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "GET ");
}

/* Send a text message */
Public function hx_send ($ sender, $ receiver, $ msg)
{
$ Url = $ this-> url. "/messages ";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
$ Data = array (
'Target _ type' => 'users ',
'Target' => array (
'0' => $ worker er
),
'Msg '=> array (
'Type' => "txt ",
'Msg '=> $ msg
),
'From' => $ sender,
'Text' => array (
'Attr1' => 'v1 ',
'Attr2' => "v2"
)
);
Return $ this-> curl ($ url, $ data, $ header, "POST ");
}
/* Query the number of offline messages to obtain the number of offline messages of an IM user */
Public function hx_msg_count ($ owner_username)
{
$ Url = $ this-> url. "/users/$ {owner_username}/offline_msg_count ";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "GET ");
}

/*
* Retrieving IM users [single]
*/
Public function hx_user_info ($ username)
{
$ Url = $ this-> url. "/users/$ {username }";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "GET ");
}
/*
* Retrieving IM users [batch]
*/
Public function hx_user_infos ($ limit)
{
$ Url = $ this-> url. "/users? $ {Limit }";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "GET ");
}
/*
* Resetting the IM User Password
*/
Public function hx_user_update_password ($ username, $ newpassword)
{
$ Url = $ this-> url. "/users/$ {username}/password ";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
$ Data ['newpassword'] = $ newpassword;
Return $ this-> curl ($ url, $ data, $ header, "PUT ");
}

/*
* Deleting an IM user [single]
*/
Public function hx_user_delete ($ username)
{
$ Url = $ this-> url. "/users/$ {username }";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
Return $ this-> curl ($ url, "", $ header, "DELETE ");
}
/*
* Modify user nickname
*/
Public function hx_user_update_nickname ($ username, $ nickname)
{
$ Url = $ this-> url. "/users/$ {username }";
$ Header = array (
'Authorization: bearer'. $ this-> token
);
$ Data ['nickname'] = $ nickname;
Return $ this-> curl ($ url, $ data, $ header, "PUT ");
}
/*
*
* Curl
*/
Private function curl ($ url, $ data, $ header = false, $ method = "POST ")
{
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
If ($ header ){
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header );
}
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, $ method );
If ($ data ){
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, json_encode ($ data ));
}
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
$ Ret = curl_exec ($ ch );
Return $ ret;
}
}

$ Rs = new Hxcall ();
// Registered user
// Echo $ rs-> hx_register ('qwerasd ', 'qazwsx', 'fuzhou 123 ');
// Add a friend to an IM user
// Echo $ rs-> hx_contacts ('admin888', 'qwerasd ');
/* Send a text message */
// Echo $ rs-> hx_send ('123456', 'admin888', 'dfadsr214wefaedf ');
/* Message count Statistics */
// Echo $ rs-> hx_msg_count ('admin888 ');
/* Obtain an IM user [single] */
// Echo $ rs-> hx_user_info ('admin888 ');
/* Get IM users [batch] */
Echo $ rs-> hx_user_infos ('20 ');
/* Delete an IM user [single] */
// Echo $ rs-> hx_user_delete ('wwwwww ');
/* Modify the user nickname */
// Echo $ rs-> hx_user_update_nickname ('axcfasdd', 'network tech ');
/* Reset the IM user password */
// Echo $ rs-> hx_user_update_password ('axcfasdd', 'asdad ');
/* Revoke the friend relationship of an IM user */
// Echo $ rs-> hx_contacts_delete ('admin888', 'qqqqqqqqqq ');
/* View friends */
// Echo $ rs-> hx_contacts_user ('admin888 ');

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.