Skip to content

Commit 7dec5e5

Browse files
committed
php api library new add
1 parent 3da86a7 commit 7dec5e5

File tree

4 files changed

+92
-51
lines changed

4 files changed

+92
-51
lines changed

example/example.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
include_once '../lib/jpush_api_php_client.php';
3+
$master_secret = '570f9aadcffe791658dde66b';
4+
$app_key='7ebc243ae2b37128472b0875';
5+
$sendno=9;
6+
7+
$client = new JpushClient($master_secret,0);
8+
9+
//send message by tag
10+
$str = $client->sendNotificationByTag('tagapi', $app_key, $sendno, 'des',
11+
'tag notify title','tag notify content', 'android','');
12+
echo $str."\n";
13+
14+
//
15+
$sendno++;
16+
$str = $client->sendCustomMesByTag('tagapi', $app_key, $sendno, 'des',
17+
'tag notify title','tag notify content', 'android','');
18+
echo $str."\n";
19+
20+
//
21+
$sendno++;
22+
$str = $client->sendNotificationByAlias('tagapi', $app_key, $sendno, 'des',
23+
'tag notify title','tag notify content', 'android','');
24+
echo $str."\n";
25+
26+
//
27+
$sendno++;
28+
$str = $client->sendCustomMesByAlias('tagapi', $app_key, $sendno, 'des',
29+
'tag notify title','tag notify content', 'android','');
30+
echo $str."\n";
31+
32+
//
33+
$sendno++;
34+
$str = $client->sendNotificationByAppkey($app_key, $sendno, 'des',
35+
'tag notify title','tag notify content', 'android','');
36+
echo $str."\n";
37+
38+
//
39+
$sendno++;
40+
$str = $client->sendCustomMesByAppkey($app_key, $sendno, 'des',
41+
'tag notify title','tag notify content', 'android','');
42+
echo $str."\n";
43+
?>

lib/jpush_api_php_client.php

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
<?php
22
include_once 'jpush_api_php_client/baseClient.php';
33

4-
class BaseClent
4+
class JpushClient
55
{
66

77
/**
88
*
99
* @var unknown
1010
*/
1111
private $masterSecret;
12-
private $app_key ;
13-
private $timeToLive ;
12+
private $time_to_live ;
1413

1514
/**
1615
* 构造函数
1716
* @param String $appKey
1817
* @param String $masterSecret
1918
* @param int $timeToLive
2019
*/
21-
public function __construct($app_key, $masterSecret, $timeToLive)
20+
public function __construct($masterSecret, $timeToLive)
2221
{
23-
$this->app_key = $app_key;
2422
$this->masterSecret = $masterSecret;
25-
$this->timeToLive = $timeToLive;
23+
$this->time_to_live = $timeToLive;
2624
}
2725

2826
/**
@@ -37,18 +35,17 @@ public function __construct($app_key, $masterSecret, $timeToLive)
3735
* @param Strng $extras
3836
*/
3937
public function sendNotificationByTag($tag, $app_key, $sendno, $send_description,
40-
$mes_title, $mes_content, $perform, $extras,$override_msg_id='')
38+
$mes_title, $mes_content, $platform, $extras='',$override_msg_id='')
4139
{
4240
$mes_type = 1;
4341
$msg_content = $this->getContent($mes_title, $mes_content, $mes_type, $extras);
44-
$receiver_value = tag
42+
$receiver_value = $tag;
4543
$baseClent = new BaseClent();
4644
$receiver_type = 2;
47-
$msg_type = 1;
4845
$verification_code = $this->getVerification_code($sendno, $receiver_type, $receiver_value);
4946
$return_str = $baseClent->send($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
50-
$msg_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
51-
echo $return_str;
47+
$mes_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
48+
return $return_str;
5249
}
5350

5451
/**
@@ -63,18 +60,17 @@ public function sendNotificationByTag($tag, $app_key, $sendno, $send_descriptio
6360
* @param Strng $extras
6461
*/
6562
public function sendCustomMesByTag($tag, $app_key, $sendno, $send_description,
66-
$mes_title, $mes_content, $perform, $extras)
63+
$mes_title, $mes_content, $platform, $extras='',$override_msg_id='')
6764
{
6865
$mes_type = 2;
6966
$msg_content = $this->getContent($mes_title, $mes_content, $mes_type, $extras);
70-
$receiver_value = tag
67+
$receiver_value = $tag;
7168
$baseClent = new BaseClent();
7269
$receiver_type = 2;
73-
$msg_type = 1;
7470
$verification_code = $this->getVerification_code($sendno, $receiver_type, $receiver_value);
7571
$return_str = $baseClent->send($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
76-
$msg_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
77-
echo $return_str;
72+
$mes_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
73+
return $return_str;
7874

7975
}
8076

@@ -90,18 +86,17 @@ public function sendCustomMesByTag($tag, $app_key, $sendno, $send_description,
9086
* @param Strng $extras
9187
*/
9288
public function sendNotificationByAlias($alias, $app_key, $sendno, $send_description,
93-
$mes_title, $mes_content, $perform, $extras)
89+
$mes_title, $mes_content, $platform, $extras='',$override_msg_id='')
9490
{
9591
$mes_type = 1;
9692
$msg_content = $this->getContent($mes_title, $mes_content, $mes_type, $extras);
97-
$receiver_value = tag
93+
$receiver_value = $alias;
9894
$baseClent = new BaseClent();
99-
$receiver_type = 2;
100-
$msg_type = 1;
95+
$receiver_type = 3;
10196
$verification_code = $this->getVerification_code($sendno, $receiver_type, $receiver_value);
10297
$return_str = $baseClent->send($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
103-
$msg_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
104-
echo $return_str;
98+
$mes_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
99+
return $return_str;
105100

106101
}
107102

@@ -117,18 +112,17 @@ public function sendNotificationByAlias($alias, $app_key, $sendno, $send_descri
117112
* @param Strng $extras
118113
*/
119114
public function sendCustomMesByAlias($alias, $app_key, $sendno, $send_description,
120-
$mes_title, $mes_content, $perform, $extras)
115+
$mes_title, $mes_content, $platform, $extras='',$override_msg_id='')
121116
{
122117
$mes_type =2;
123118
$msg_content = $this->getContent($mes_title, $mes_content, $mes_type, $extras);
124-
$receiver_value = tag
119+
$receiver_value = $alias;
125120
$baseClent = new BaseClent();
126-
$receiver_type = 2;
127-
$msg_type = 1;
121+
$receiver_type = 3;
128122
$verification_code = $this->getVerification_code($sendno, $receiver_type, $receiver_value);
129123
$return_str = $baseClent->send($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
130-
$msg_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
131-
echo $return_str;
124+
$mes_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
125+
return $return_str;
132126

133127
}
134128

@@ -143,18 +137,17 @@ public function sendCustomMesByAlias($alias, $app_key, $sendno, $send_descripti
143137
* @param Strng $extras
144138
*/
145139
public function sendNotificationByAppkey($app_key, $sendno, $send_description,
146-
$mes_title, $mes_content, $perform, $extras)
140+
$mes_title, $mes_content, $platform, $extras='',$override_msg_id='')
147141
{
148142
$mes_type = 1;
149143
$msg_content = $this->getContent($mes_title, $mes_content, $mes_type, $extras);
150-
$receiver_value = tag
144+
$receiver_value = '';
151145
$baseClent = new BaseClent();
152-
$receiver_type = 2;
153-
$msg_type = 1;
146+
$receiver_type = 4;
154147
$verification_code = $this->getVerification_code($sendno, $receiver_type, $receiver_value);
155148
$return_str = $baseClent->send($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
156-
$msg_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
157-
echo $return_str;
149+
$mes_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
150+
return $return_str;
158151

159152
}
160153

@@ -169,18 +162,17 @@ public function sendNotificationByAppkey($app_key, $sendno, $send_description,
169162
* @param Strng $extras
170163
*/
171164
public function sendCustomMesByAppkey($app_key, $sendno, $send_description,
172-
$mes_title, $mes_content, $perform, $extras)
165+
$mes_title, $mes_content, $platform, $extras='',$override_msg_id='')
173166
{
174167
$mes_type = 2;
175168
$msg_content = $this->getContent($mes_title, $mes_content, $mes_type, $extras);
176-
$receiver_value = tag
169+
$receiver_value = '';
177170
$baseClent = new BaseClent();
178-
$receiver_type = 2;
179-
$msg_type = 1;
171+
$receiver_type = 4;
180172
$verification_code = $this->getVerification_code($sendno, $receiver_type, $receiver_value);
181173
$return_str = $baseClent->send($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
182-
$msg_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
183-
echo $return_str;
174+
$mes_type, $msg_content, $send_description, $platform, $this->time_to_live, $override_msg_id);
175+
return $return_str;
184176

185177
}
186178

@@ -194,6 +186,7 @@ public function sendCustomMesByAppkey($app_key, $sendno, $send_description,
194186
*/
195187
private function getContent($mes_title, $mes_content, $mes_type, $extras)
196188
{
189+
//echo "mes_title=".$mes_title."mes_content=".$mes_content."mes_type=".$mes_type."extras=".$extras;
197190
$content_str = '';
198191
if($mes_type == 1)
199192
{
@@ -205,13 +198,14 @@ private function getContent($mes_title, $mes_content, $mes_type, $extras)
205198
$content = array('title'=>$mes_title, 'message'=>$mes_content, 'extras'=>$extras);
206199
$content_str = json_encode($content);
207200
}
201+
//echo $content_str;
208202
return $content_str;
209203
}
210204

211205

212206
private function getVerification_code($sendno, $receiver_type, $receiver_value)
213207
{
214-
$verification_str = $sendno.$receiver_type.$receiver_value.$this->$masterSecret;
208+
$verification_str = $sendno.$receiver_type.$receiver_value.$this->masterSecret;
215209
$verification_str = md5($verification_str);
216210
return $verification_str;
217211
}

lib/jpush_api_php_client/baseClient.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ public function send($sendno, $app_key, $receiver_type, $receiver_value, $verifi
2929
$msg_type, $msg_content, $send_description, $platform, $time_to_live, $override_msg_id)
3030
{
3131
$params = $this->getParams($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
32-
$msg_type, $msg_content, $send_description, $platform, $time_to_live, $override_msg_id)
33-
$this->httpPostClient = new HttpPostClient();
32+
$msg_type, $msg_content, $send_description, $platform, $time_to_live, $override_msg_id);
33+
$httpPostClient = new HttpPostClient();
3434
return $httpPostClient->request_post($this->API_URL, $params);
3535
}
3636

3737
private function getParams($sendno, $app_key, $receiver_type, $receiver_value, $verification_code,
3838
$msg_type, $msg_content, $send_description, $platform, $time_to_live, $override_msg_id)
3939
{
40-
$params = "sendno".$sendno."&app_key".$app_key."&receiver_type".$receiver_type.
41-
"&receiver_value".$receiver_value."&verification_code".$verification_code.
42-
"&msg_type".$msg_type."&msg_content".$msg_content."&send_description".$send_description.
43-
"&send_description".$send_description."&platform".$platform.
44-
"&time_to_live.".$time_to_live."&override_msg_id".$override_msg_id;
40+
$params = "app_key=".$app_key."&receiver_type=".$receiver_type.
41+
"&receiver_value=".$receiver_value."&verification_code=".$verification_code.
42+
"&msg_type=".$msg_type."&msg_content=".$msg_content."&send_description=".$send_description.
43+
"&send_description=".$send_description."&platform=".$platform.
44+
"&time_to_live=".$time_to_live."&override_msg_id=".$override_msg_id."&sendno=".$sendno;
45+
//echo $params."\n";
4546
return $params;
4647
}
4748
}

lib/jpush_api_php_client/httpPostClient.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ function request_post($remote_server, $post_string)
1414
$context = array(
1515
'http' => array(
1616
'method' => 'POST',
17-
'header' => 'Content-type: application/x-www-form-urlencoded' .
18-
'\r\n'.'User-Agent : Jimmy\'s POST Example beta' .
17+
'header' => 'Connection:Keep-Alive'.
18+
'\r\n'.'Charset: UTF-8' .
19+
'\r\n'.'Content-type: application/x-www-form-urlencoded'.
1920
'\r\n'.'Content-length:' . strlen($post_string) + 8,
20-
'content' => 'mypost=' . $post_string)
21+
'content' => $post_string)
2122
);
23+
//echo $post_string;
2224
$stream_context = stream_context_create($context);
25+
//echo $stream_context;
2326
$data = file_get_contents($remote_server, false, $stream_context);
2427
return $data;
2528
}

0 commit comments

Comments
 (0)