Heim >Backend-Entwicklung >PHP-Tutorial >Nachrichten-Push, Echtzeit-Chat

Nachrichten-Push, Echtzeit-Chat

WBOY
WBOYOriginal
2016-07-29 09:10:481132Durchsuche
<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>

Das Obige hat den Nachrichten-Push und den Echtzeit-Chat eingeführt, einschließlich einiger Aspekte davon. Ich hoffe, dass es für Freunde hilfreich sein wird, die sich für PHP-Tutorials interessieren.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn