-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_calender.php
92 lines (74 loc) · 3.08 KB
/
post_calender.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
function sendPostRequest($postargs,$token, $cal){
$APIKEY = 'AIzaSyCBgCHben7bTtLRf3TA0bAhBtGnBUtRI5Q';
$request = 'https://www.googleapis.com/calendar/v3/calendars/' . $cal . '/events?pp=1&key=' . $APIKEY;
//$auth = json_decode($_SESSION['oauth_access_token'],true);
//var_dump($auth);
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, true);
curl_setopt($session, CURLINFO_HEADER_OUT, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $token,'X-JavaScript-User-Agent: Mount Pearl Tennis Club Bookings'));
$response = curl_exec($session);
//echo '<pre>';
//var_dump(curl_getinfo($session, CURLINFO_HEADER_OUT));
//echo '</pre>';
curl_close($session);
return $response;
}
function createPostArgsJSON($sdate,$edate,$starttime,$endtime,$title,$where,$request){
$arg_list = func_get_args();
foreach($arg_list as $key => $arg){
$arg_list[$key] = urlencode($arg);
}
$postargs = <<<JSON
{
"start": {
"dateTime": "{$sdate}T{$starttime}:00.000+05:30"
},
"end": {
"dateTime": "{$edate}T{$endtime}:00.000+05:30"
},
"summary": "$title",
"description": "$request",
"location": "$where"
}
JSON;
return $postargs;
}
//
//-Post value to calender
//
$date = "01/26/2017";
$enddate =$startdate = date("Y-m-d", strtotime($date));
$starttime = '05:00';
$endtime ='18:00';
$calendarId ="[email protected]";
$postargs = createPostArgsJSON($startdate,$enddate,$starttime,$endtime,"Test Calender","Mangalur","Hi");
//print_r($postargs);
$tokenURL = 'https://accounts.google.com/o/oauth2/token';
$postData = array(
'client_secret'=>'TbsQWnKPRUkhCdjYFMjrMKRX',
'grant_type'=>'refresh_token',
'refresh_token'=>'1/kdKV10PnkM7y0WeAmC9fF1Lznwn5MBhn1P3CwvYd0a252YlhgT6hM3tTWan-5bYj',
'client_id'=>'1096366798109-89j8o33jcnhb8id6hlcu51gvuhu6r0fd.apps.googleusercontent.com'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tokenReturn = curl_exec($ch);
$token = json_decode($tokenReturn);
//var_dump($tokenReturn);
$accessToken = $token->access_token;
$token = $accessToken;
$result = sendPostRequest($postargs,$token,$calendarId);
echo '<pre>' . $result . '</pre>';
?>