Rumah >pembangunan bahagian belakang >tutorial php >消息推送,实时聊天

消息推送,实时聊天

WBOY
WBOYasal
2016-07-29 09:10:481132semak imbas
<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教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn