【小(xiǎo)編推薦】極光(guāng)推送phpγ$♥推送代碼

2016-01-03   |&nbεπsp;  發布者:梁國(guó)芳  &‌× |   查看(kàn):3320次 ±

php
由于v3代碼,各種報(bào)錯(cuò),各種坑,所以去(qù)網上(sh±©¥'àng)找了(le)一(yī)些(xiē)神奇的(de)代碼,略微(wēi↓↓)改動後,成功推送。   
$pushObj = new Jpush();
    //組裝需要(yào)的(<β₩de)參數(shù)
    $receive = 'all'; &nbs€≠p;   //全部
    //$receive = arr$↑☆€ay('tag'=>array('24≈₹01','2588','9527'));     ∑>↓ //标簽
  //  $receive = array('ali≈≥™as'=>array('93d78b736™∏11d886a74*****88497f501')≥♦₽§);    //别名
    $content = '這(☆ >zhè)是(shì)一(yī)個(gè)測試的(de)推σ≥∞"送數(shù)據....測試....Hello World.λ÷↔..';
    $m_type = 'h↑φ♠​ttp';
    $m_txt = 'http://www."↓iqujing.com/';
    $m_time = '600'; '      §®  //離(lí)線保留時(shí)間(jiā÷↕↕♦n)
 
    //調用(yòng)推送,并處理λπ>(lǐ)
    $result = $pushObj-≤✘≈>push($receive,$cont≠Ω£‌ent,$m_type,$m_txt,$m_ti×♥‍∞me);
    if($result){€±±
        $res_arr =♣↕ json_decode($result,↓​ true);
        if(×∑γ§isset($res_arr['error'])){  ×☆>©        <™ ;       &n¶≠≠×bsp;     //如(r∑βú)果返回了(le)error則證明(míng)失敗
        &  ®nbsp;   echo $res_arr[' ♦₽error']['message'];   &nb♠€®sp;      //錯(¥∞αcuò)誤信息
        &n♣↕©₹bsp;   echo $res ≈_arr['error']['code']; ™≤Ω         ♦♣σσ;   //錯(cuò)誤碼
          ∑ §£  return false;β<<→       
        }else{≤&
      ✘λ★      //處理(lσπ​βǐ)成功的(de)推送......
       ✔®₽     echo '™✔sucess.....';
         ¥ε   return true;
       ←™₩ }
    }else{     &nbs ♠¥✔p;//接口調用(yòng)失敗或無響應
        ech¥©$o '接口調用(yòng)失敗或無響應';
       ¶≤≤ return false;
    }
 
 
class Jpush{
  
    private $app_key =™ ← '00c8e702649f75c6a263017e§€';       α↑¶    //待發送的(✘∞de)應用(yòng)程序(appKey),隻能(néng←♣<₩)填一(yī)個(gè)。
    private $m©≈¥☆aster_secret = 'badfdb57256dd342d84d6£€87b';      //主密碼
    private $url = "♥';https://api.jpush.cn/♦®&αv3/push";     &×₽¶nbsp;//推送的(de)地(dì)址
 
    //若實例化(huà)的(de)時(shí)候傳入↔₩相(xiàng)應的(de)值則按新的(de)相(xiàng)應值進¥€™行(xíng)
    public function __constru≈↓ct($app_key=null, $master§÷δ∑_secret=null,$url=null) {
        if ($a♣★₹pp_key) $this->ap↑↑©•p_key = $app_key;
      &nb©↔sp; if ($master_secret) $this-→→>master_secret = $master_secret€<;
        if​≤ ($url) $this->url = $₽‍  url;
    }
 
    /*  $rec↑α≠eiver 接收者的(de)信息
        all 字符串‍∞ 該産品下(xià)面的(de)所有(yǒu)↑÷用(yòng)戶. 對(duì)app_ke↔β‌♠y下(xià)的(de)所有(yǒu)用(♥×®¥yòng)戶推送消息
        tag(20個♦✘÷♣(gè))Array标簽組(并集): t§♦>ag=>array('昆明(míng)','北(běi ← ≈)京','曲靖','上(shàng)海(hǎ↓≠​i)');
        tag€ε₩_and(20個(gè))Array标簽組(交集): ta•←™g_and=>array('廣州','女(nǚ)×‍');
        alias→σ(1000)Array别名(并集): a→ ≈βlias=>array('93d78b73611d8÷¥δ86a74*****88497f501','606d0509089≈×₽₩6228f66ae10d1*****310'→¶);
        registra₩₽ tion_id(1000)注冊ID設備标識(并φ₩§<集): registration_id=>array('20£♠effc071de0b45c1a*********>★Ω>*2824746e1ff2001bd80308a467d800bed39±β≤♦e');
    */
    //$content 推送的(de&↓)內(nèi)容。
    //$m_typeλ' 推送附加字段的(de)類型(可(kě)不(bù)填) ht↔↑♠tp,tips,chat....
    //$m_txt 推送附加字段的(de ©)類型對(duì)應的(de)內(nèi)容(可(kě)不‌§(bù)填) 可(kě)能(néng)是(shì)url,可(kě)∏♥能(néng)是(shì)一(yī)段文(wén)字。
    //$m_time 保存離(lí)線時(shí)間α¶'∑(jiān)的(de)秒(miǎo)數(shù)默認為(wèi)一(yī<&)天(可(kě)不(bù)傳)單位為(wèi)秒(miǎo)
    public fu≤δ∞∞nction push($receiver='✔ εall',$content='',$m_type='',$m_txt★₩='',$m_time='86400'){
      &nbs‍£π p; $base64=base64_encode("₹π ∏$this->app_key:$th≠↑is->master_secret");
       α∏∞← $header=array("Au•©thorization:Basic $base64&q♦±uot;,"Content-Type:a±∏​δpplication/json");
      •↕  $data = array();
        $data[←¥ε'platform'] = 'all';    β☆      ∞β‍®;//目标用(yòng)戶終端手機(jī)的(de© ↑)平台類型android,ios,winphone
       ★↑≤  $data['audience'] = $receiver;  ‍✘≥;    //目标用(yòng)戶
      &nπ≈φ€bsp;  
     ≈    $data['notification'] = array(
          &n♥↑₩×bsp; //統一(yī)的(de)模式--标準模式
          &nbs♦£p; "alert"=>$coα​≈‍ntent,   
        &nbs¶↓p;   //安卓自(zì)定義
        δπ    "android"=&←®✔gt;array(
          &nb↓  ♠sp;     "alert"=∞φ•≈>$content,
          &'§nbsp;     "title&quoΩ™σ t;=>"",
        &nb↓↓¶sp;       &qu✘≠δot;builder_id"=>1,
          &nb >±sp;     "extras"←σ=>array("type"☆♠↔↕=>$m_type, "txt" &∑©;=>$m_txt)
       ✘‌•π;     ),
       &→     //ios÷★的(de)自(zì)定義
        &♣ nbsp;   "ios"↓₽α♥;=>array(
     ↔÷          &nbs​∞§p; // "alert"¥≠±;=>$content,
          &nb✘÷sp;     "®δ♥badge"=>"1",
          &n₩>bsp;     "soun ¥®d"=>"default",
        &nbs±£φp;       ∏€₹// "extras"=>array(♣φγ®"type"=>$m_type, " ≈≈txt"=>$m_txt)
        &π•✔nbsp;   ),
        );
 
        £↓       //✔≥→蘋果自(zì)定義---為(wèi)了(≥₽le)彈出值方便調測
        $data['mess ∑age'] = array(
       α©←​     "msg_content&φ∞quot;=>$content,
       ☆λ♠→;     "extra£¥s"=>array("✘ type"=>$m_type, &q≈✘←↔uot;txt"=>$m_txt)
       ‌ε; );
 
        //附加選項
       €<♥♣ $data['options'] = array(
       ↑↓£;     "sendno"= ♣§≈>time(),
          &n‌ bsp; "time_to_live✘β"=>$m_time, ↑& //保存離(lí)線時(shí)間(jiān)的(de)秒(miǎo★∏<)數(shù)默認為(wèi)一(yī)天
       ∞<¥Ω;     "ap★<ns_production"=&g§‍t;1,        //指定 A≈©PNS 通(tōng)知(zhī)發送環境:0開(kāi)發環π©境,1生(shēng)産環境。
        );
        $para↑÷↔¶m = json_encode($dat♠♠ a);
       ♣¥; $res = $this->push_curl($param,$h♦↕☆₩eader);
         ↓®
        if($res)φ₩{       /★€£ /得(de)到(dào)返回值--成功已否後面判斷
        &nb&≈‌ sp;   return $reγ s;
      &nbπ©sp; }else{      ‍≈✘     //未得(d ←e)到(dào)返回值--返回失敗
        &n ₽★∏bsp;   return fal₽ se;
        ‌↓}
    }
 
    //推送的(de)Curl方法
    public function push_∏™™curl($param=""Ω€;,$header="") {
        if π★ ↓(empty($param)) { ret€€∞®urn false; }
       ✔$   $postUrl = $this->ur©¶≥σl;
        $curlPost =" ✔γ $param;
        $ch∞✔≤✘ = curl_init();     &n£←≥bsp;     &nb± ‍sp;       Ω↑©∏        &​'↕nbsp;        λ©  //初始化(huà)curl
        curl_set∑≥opt($ch, CURLOPT_URL,$postπ‌Url);       &nbs→≈↑₽p;      δ∏   //抓取指定網頁
       ©↕ curl_setopt($ch, CURLOPT_HEADER, 0); &&←±nbsp;     &nbs€∏≈'p;       &n✔↔bsp;    //設置header
      &nb∞"sp; curl_setopt($ch, CURLOPT♥★Ω☆_RETURNTRANSFER, 1);   &nb↑♣β×sp;        //要∏§‌(yào)求結果為(wèi)字符串且輸出到(dào)屏幕©↑€上(shàng)
        curl_setopt®φ ($ch, CURLOPT_POST, 1∑$÷);     &nb<₩sp;     &nb≤→♦↕sp;         ↑∏α‍ //post提交方式
        curl_α×±©setopt($ch, CURLOPT_POSTFIELDS, $curlP¥γ'₹ost);
     ₽♥↕   curl_setopt($ch, CUγ ↓RLOPT_HTTPHEADER,$header);   &nb↑↓₽sp;      ≥§♠∞ // 增加 HTTP Header(頭)裡(lǐ)的(de)字段 ←&→•;
        &± ™curl_setopt($ch, CURLOPT_SSL_VERI£↓FYPEER, FALSE);       βλ // 終止從(cóng)服務端進行(xíng)φ♥驗證
        λ± curl_setopt($ch, CURLOPT_SSL_VERIFYHOS$©↓T, FALSE);
        $data = cur ×l_exec($ch);    ↑₽          & φ​↑nbsp;         &nε×bsp;       &nbs>σβ←p; //運行(xíng)curl
       ≠ππ∑; curl_close($ch);
        retσ™​×urn $data;
    }
}以上(shàng)代碼也(yě)可(kě)以下(xià‍÷$♠)載 : /down/show.ph§ ∞p?itemid=20