<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 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.