Browse Source

umeng test

liax 9 years ago
parent
commit
533a95b33b
1 changed files with 59 additions and 16 deletions
  1. 59 16
      helper/umeng.php

+ 59 - 16
helper/umeng.php

@@ -19,6 +19,19 @@ class msg_type extends SplEnum
     const file_id = "file_id";
 }
 
+class status_code extends SplEnum
+{
+    const __default = self::OK;
+    const OK = 200;
+    const CREATED = 201;
+    const ACCEPTED = 202;
+    const BAD_REQUEST = 400;
+    const UNAUTHORIZED = 401;
+    const FORBIDDEN = 403;
+    const NOT_FOUND = 404;
+    const INTERNAL_SERVICE_ERROR = 500;
+}
+
 class display_type extends SplEnum
 {
     const __default = self::notification;
@@ -26,19 +39,40 @@ class display_type extends SplEnum
     const message = "message";
 }
 
-
+/**
+ * Class upush
+ *
+ * 友盟推送内容
+ */
 class upush
 {
     const PUSH_URL = "http://msg.umeng.com/api/send";
-    const METHOD = "POST";
     const APP_MASTER_SECRET = "r6w2a8z9x8zonh7qmk8ds2fvypu02wpj";
     const APPKEY = "5631efd4e0f55a8770000027";
 
+    private $type;
+    private $alias_type;
+    private $alias;
+    private $ticker;
+    private $title;
+    private $text;
+
+
     public function __construct()
     {
+        $type = msg_type::unicast;
+        $ticker = "ticker";
+        $title = "title";
+        $text = "text";
     }
 
-    // post 请求发送
+    /**
+     * post发送数据
+     *
+     * @param $url
+     * @param $data_string
+     * @return array
+     */
     public function http_post_data($url, $data_string)
     {
         $ch = curl_init();
@@ -57,13 +91,27 @@ class upush
         return array($return_code, $return_content);
     }
 
+    /**
+     * 生成签名
+     *
+     * @param $http_method
+     * @param $url
+     * @param $post_body
+     * @param $app_master_secret
+     * @return string
+     */
+    private function make_sign($http_method, $url, $post_body, $app_master_secret)
+    {
+        return strtolower(md5($http_method . $url . json_encode($post_body) . self::APP_MASTER_SECRET));
+    }
+
     // android 打包
     private function android_pack()
     {
         $pack = array();
-        $pack['appkey'] = "5631efd4e0f55a8770000027";
+        $pack['appkey'] = self::APPKEY;
         $pack['timestamp'] = time();
-        $pack['type'] = msg_type::unicast;
+        $pack['type'] = $this->type;
         $pack['device_tokens'] = "Aj9AympPsCha5zmPhrV0DbnOZJYF0pqKY5jdKvFy_Hbu";
         $pack['alias_type'] = "";
         $pack['alias'] = "";
@@ -71,9 +119,9 @@ class upush
         $pack['filter'] = "";
         $payload = array();
         $body = array();
-        $body['ticker'] = "通知栏提示文字";
-        $body['title'] = "通知图标";
-        $body['text'] = "通知文字描述";
+        $body['ticker'] = $this->ticker;
+        $body['title'] = $this->title;
+        $body['text'] = $this->text;
         $body['icon'] = "";
         $body['largeIcon'] = "";
         $body['img'] = "";
@@ -114,19 +162,14 @@ class upush
     // ios 打包
     private function ios_pack()
     {
-
-    }
-
-    private function make_sign(){
-
-        return strtolower(md5(self::METHOD.self::PUSH_URL.json_encode(self::android_pack()).self::APP_MASTER_SECRET));
     }
 
     // android 推送
     public function android_push()
     {
-//        $sign = strtolower(md5(self::METHOD.self::PUSH_URL.self::android_pack().self::APP_MASTER_SECRET,false));
-        return $this->http_post_data(self::PUSH_URL.'?sign='.$this->make_sign(), json_encode(self::android_pack()));
+        $pack_body = self::android_pack();
+        $url = self::PUSH_URL . '?sign=' . self::make_sign("POST", self::PUSH_URL, $pack_body, self::APP_MASTER_SECRET);
+        return $this->http_post_data($url, json_encode($pack_body));
     }
 
     // ios 推送