-
Notifications
You must be signed in to change notification settings - Fork 18
/
push.php
55 lines (44 loc) · 1.58 KB
/
push.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
//从百度服务器获取授权,oauth doc http://developer.baidu.com/wiki/index.php?title=docs/oauth/client
$tokenUrl = "https://openapi.baidu.com/oauth/2.0/token?";
$query = array(
"grant_type"=>"client_credentials",
"client_id"=>"I58ECFzaGKYyy2LR3sHB2pis",
"client_secret"=>"FPNHh51ZRUdipMEucfTwLVyGlCcDShUU",
"scope"=>""
);
$url = $tokenUrl.http_build_query($query);
$authorization = file_get_contents($url);
$data = json_decode($authorization,1);
//var_dump($data);
//开始消息推送 doc https://openapi.baidu.com/rest/2.0/lightapp/msg/push
$pushUrl = "https://openapi.baidu.com/rest/2.0/lightapp/msg/push";
$access_token = $data['access_token'];
$type = isset($_POST['type']) && $_POST['type'] == 1?1:2;
$push_token = $_POST['push_token'];
$tag = $_POST['tag'];
$title = isset($_POST['title'])?$_POST['title']:'welcome to clouda world';
$content = isset($_POST['content'])?$_POST['content']:'clouda is perfect! timestamp:'.time();
$url = isset($_POST['url'])?$_POST['url']:'http://blenddemo.duapp.com';
isset($tag) && $content = "订阅Tag:".$tag."|".$content;
$params = array(
"access_token"=>$access_token,
"type"=>$type,
"push_token"=>$push_token,
"tag"=>$tag,
"title"=>$title,
"content"=>$content,
"url"=>$url
);
function post($uri,$params){
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $uri);
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
echo post($pushUrl,$params);