Home >Backend Development >PHP Tutorial >Message push, real-time chat

Message push, real-time chat

WBOY
WBOYOriginal
2016-07-29 09:10:481132browse
<span><em>//</em></span><span><em>消息推送
</em></span><span>function </span><span>notice_message_push(</span><span>$data</span><span>){</span>
<pre class="brush:php;toolbar:false"><pre class="brush:php;toolbar:false"><span><em>//     $tag = '"all"';  发给所有用户</em></span><span><em></em></span><span><em>//     $tag = '{"tag":["'.$uid.'"]}';   发给部分用户</em></span>
<span><em>//     $data = '{"platform": "all","audience" :'.$tag.',"notification":{"android" : {"alert" : "'.$content.'", "title":"'.$title.'","sound":"default", "badge":"+1","extras":{"newsid":321}},"ios":{"alert":"'.$content.'","title":"'.$title.'","sound":"default","badge":"+1","extras":{"newsid":321}}},"options":{"time_to_live":60,"apns_production":false}}';
</em></span>
<pre class="brush:php;toolbar:false"><span>    $url</span><span>= </span><span>"https://api.jpush.cn/v3/push"</span><span>;</span>
<span><em>// </em></span><span><em>启动一个</em></span><span><em>CURL</em></span><span><em>会话
</em></span><span><em></em></span><span>$curl </span><span>= </span><span><em>curl_init</em></span><span>();
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_URL</em></span><span>, </span><span>$url</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_CUSTOMREQUEST</em></span><span>, </span><span>"POST"</span><span>);</span><span><em></em></span><span><em></em></span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_SSL_VERIFYPEER</em></span><span>, </span><span>FALSE</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_SSL_VERIFYHOST</em></span><span>, </span><span>FALSE</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_USERAGENT</em></span><span>, </span><span>'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_FOLLOWLOCATION</em></span><span>, </span><span>1</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_AUTOREFERER</em></span><span>, </span><span>1</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_POSTFIELDS</em></span><span>, </span><span>$data</span><span>);
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_HTTPHEADER</em></span><span>, </span><span>array</span><span>(
</span><span>'Content-Type: application/json; charset=utf-8'</span><span>,
</span><span>'Authorization: Basic MWI0MTc3NTMxODAwMTE5MjJjMzU2MjAxOjkwMjBiZjNhNGUzNzkyNTM2YjNjYzJkNw=='
</span><span>));
</span><span><em>curl_setopt</em></span><span>(</span><span>$curl</span><span>, </span><span><em>CURLOPT_RETURNTRANSFER</em></span><span>, </span><span>true</span><span>);
</span><span><em>// </em></span><span><em>执行操作
</em></span><span><em></em></span><span>$response_body </span><span>= </span><span><em>curl_exec</em></span><span>(</span><span>$curl</span><span>);
</span><span><em>//</em></span><span><em>捕抓异常
</em></span><span><em></em></span><span>$error_msg </span><span>= </span><span>""</span><span>;
</span><span>if </span><span>(</span><span><em>curl_errno</em></span><span>(</span><span>$curl</span><span>)) {
</span><span>$error_msg </span><span>= </span><span>'Errno' </span><span>. </span><span><em>curl_error</em></span><span>(</span><span>$curl</span><span>);
</span><span>    }
</span><span><em>// </em></span><span><em>关闭</em></span><span><em>CURL</em></span><span><em>会话
</em></span><span><em></em></span><span><em>curl_close</em></span><span>(</span><span>$curl</span><span>);
</span><span><em>// </em></span><span><em>返回结果
</em></span><span><em></em></span><span>$response</span><span>[</span><span>"response_body"</span><span>] = </span><span>$response_body</span><span>;
</span><span>$response</span><span>[</span><span>"error_msg"</span><span>] = </span><span>$error_msg</span><span>;
</span><span>return </span><span>$response</span><span>;
</span><span>}</span>

The above has introduced message push and real-time chat, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn