首頁 > 後端開發 > php教程 > 訊息推播,即時聊天

訊息推播,即時聊天

WBOY
發布: 2016-07-29 09:10:48
原創
1145 人瀏覽過
<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>
登入後複製

以上就介紹了訊息推播,即時聊天,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板