# 服务端发送消息

# REST Host

香港: api.irealtime.cn

# RESTful API


URL: http(s)://<REST Host>/rest/publish

Method:

  • Post

参数:

名称 必要 描述
appkey 您的 appkey
channel channel
content 您要发送的消息内容

返回值:


{
"code" : 200,
"content" : "OK"
}

# 调用方式

irealtime 支持以下方式调用 restful API

# PHP


$url = 'https://api.irealtime.cn/rest/publish'; //推送消息的服务器地址
$appkey = "Your Rest key";  // 您的app详情里面的 Rest key
$channel = "my_channel"; // 您要发送消息的通道名称
$content = "Hello, IRealTime!"; // 您要发送的消息内容


$post_data = array("appkey"=>$appkey,"channel"=>$channel,"content"=>$content);

echo php_ajax($url,$post_data);


function php_ajax($url, $post_data) {
  $postdata = http_build_query($post_data);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/x-www-form-urlencoded',
      'content' => $postdata,
      'timeout' => 15 * 60 
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);

  return $result;
}

# NodeJS

var https = require('https')

var appkey = 'Your Rest key' // 您的app详情里面的 Rest key
var channel = 'my_channel'   // 您要发送消息的通道名称
var content = 'Hello, IRealTime!'  // 您要发送的消息内容

var queryParams =
  'appkey=' + appkey + '&channel=' + channel + '&content=' + content

var options = {
  hostname: 'api.irealtime.cn',
  path: '/rest/publish',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
}

var req = https.request(options, (res) => {
  res.setEncoding('utf8')
  res.on('data', (result) => {
    console.log(`响应结果: ${result}`)
  })
})

req.on('error', (e) => {
  console.error(e)
})

req.write(queryParams)
req.end()

# Python


appkey = "your_appkey"  # 您的app详情里面的 Rest key 
channel = "my_channel"  # 您要发送消息的通道名称
content = "Hello, IRealTime!" # 您要发送的消息内容
data = {"appkey" : appkey, "channel" : channel, "content" : content}
url = "https://api.irealtime.cn/rest/publish"
response = requests.post(url, data = data)


# C#

string url = "https://api.irealtime.cn/rest/publish";
string appkey = "your_appkey"; // 您的app详情里面的 Rest key 
string channel = "my_channel"; // 您要发送消息的通道名称 
string content = "Hello, IRealTime!"; // 您要发送的消息内容 
string postDataStr = "appkey="+appkey+"&channel="+channel+"&content="+content;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
Stream myRequestStream = request.GetRequestStream();
byte[] data = Encoding.UTF8.GetBytes(postDataStr);
myRequestStream.Write(data, 0, data.Length);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

# Ruby


require 'net/http'

class Client
  def self.send(channel, content)
    Thread.new {
      Net::HTTP.post_form(URI("https://api.irealtime.cn/rest/publish"),
       {appkey: "your_appkey", channel: channel, content: content})
    }
  end
end