ทริปนี้ เราจะแอบเอาความสามารถของ google calendar มาใช้ หลายท่านคงทราบอยู่แล้วว่า google calendar นั้น มีฟังก์ชั่นเตือนเหตุการณ์ล่วงหน้า  ด้วย SMS และมี API ให้เราใช้  เราจะใช้ช่องตรงนี้แหละครับ  มาทำให้โปรแกรมเราส่ง SMS หาเราเมื่อมีคอมเม้นท์ใหม่ หรือว่ามีเหตุการณ์อันใดเกิดขึ้น สักอย่างกับเว็บเรา ลองดูโมเดล คร่าวๆ

 

send SMS model

 

1.ต้องมีแอคเค้าของ google และ เซต SMS

 

สำหรับใช้ google calendar และต้องตั้งให้ google calendar ส่ง SMS หาเราได้ ผมคงจะไม่กล่าวขั้นตอนนี้เอง เพราะมีท่านอื่นๆ ได้เขียนไว้ละเอียดแล้ว สามารถอ่านได้ที่

http://www.9tana.com/node/google-calendar/ หรือที่นี่ http://gotoknow.org/blog/krunapon/118353 ย้ำนะครับว่าต้องเซ็ตขั้นตอนนี้ให้เรียบร้อย สำหรับบางเครือข่ายโทรศัพท์ ไม่สามารถใช้คุณสมบัตินี้ได้ เพราะ google calendar ยังไม่ซัพพอร์ต ก็ต้องแสดงคำว่า เสียใจด้วย แต่ถ้าอยากจะทดสอบ ทดลอง เอาโดเรม่อนไปแลกซิม ทรูมูฟ จากเซเว่นมาสักซิม ก็ได้ครับ

 

 

2.ดาวน์โหลด Zend Gdata

ไลบรารี่นี้ทำให้เราเขียนติดต่อกับ google calendar ได้ ง่ายและสะดวก

ZendGdata-1.7.3.zip

 

 

3.แตกซิป Zend Gdata

ในโฟลเดอร์ ZendGdata-1.7.2 จะมีทั้ง document และโค้ดตัวอย่างมากมาย  แต่สิ่งที่เราต้องใช้คือสิ่งที่อยู่ในโฟลเดอร์ ZendGdata-1.7.2libraryZend ก้อปปี้มาใช้ทั้งโฟลเดอร์ Zend เลยนะครับ

 

 

4.สร้างฟังก์ชั่นสำหรับติดต่อกับ google calendar

มันก็คือฟังก์ชั่นที่ใช้เพิ่มรายการ เข้าไปใน google calendar และตั้งเวลาให้ส่ง SMS หาเรา โดยในฟังก์ชั่นนี้ผม ให้ใส่รายการไป ณ.เวลาปัจจุบัน+3 นาที และให้แจ้งเตือนก่อนถึงรายการนี้ 2 นาที เพราะฉะนั้นเมื่อ + – แล้ว หลังสคริปเรารันจบ ประมาณ 1 นาที จะต้องมี SMS หาเรา

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
<?php
function send_sms( $param_google_username , $param_google_password,$param_title,$param_content ){
 
	/**
	 * @Include Zend_Loader
	 */
	require_once 'Zend/Loader.php';
 
	/**
	 * @Load Zend_Gdata
	 */
	Zend_Loader::loadClass('Zend_Gdata');
 
	/**
	 * @Load Zend_Gdata_AuthSub
	 */
	Zend_Loader::loadClass('Zend_Gdata_AuthSub');
 
	/**
	 * @Load Zend_Gdata_ClientLogin
	 */
	Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
 
	/**
	 * @Load Zend_Gdata_HttpClient
	 */
	Zend_Loader::loadClass('Zend_Gdata_HttpClient');
 
	/**
	 * @Load Zend_Gdata_Calendar
	 */
	Zend_Loader::loadClass('Zend_Gdata_Calendar');
 
	// Parameters for ClientAuth authentication
	$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
	$user = $param_google_username ;
	$pass =$param_google_password ;
 
	// Create an authenticated HTTP client
	$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
 
	// Create an instance of the Calendar service
	$service = new Zend_Gdata_Calendar($client);
 
	// Create a new entry using the calendar service's magic factory method
	$event= $service->newEventEntry();
 
	// Populate the event with the desired information
	// Note that each attribute is crated as an instance of a matching class
	$event->title = $service->newTitle( $param_title );
 
	//$event->where = array($service->newWhere("Mountain View, California"));
	$event->content = $service->newContent( $param_content );
 
	// Set the date using RFC 3339 format.
	$startDate = date( "Y-m-d" );//"2009-01-15";
	$startTime = date( "H:i" , strtotime("+3 minutes") );
	$endDate = date( "Y-m-d" );
	$endTime = date( "H:i" , strtotime("+6 minutes") );
	$tzOffset = "+07";
 
	$when = $service->newWhen();
	$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
	$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
 
	// Create a new reminder object. It should be set to send an email
	// to the user 10 minutes beforehand.
	$reminder = $service->newReminder();
	$reminder->method = "sms";
	$reminder->minutes = "2";
 
	$when->reminders = array($reminder);
 
	$event->when = array($when);
 
	// Upload the event to the calendar server
	// A copy of the event as it is recorded on the server is returned
	if ($newEvent = $service->insertEvent($event)){
 
		return true ;
	}else{
 
		return false;
	}
 
}?>

 

ฟังก์ชั่นนี้ผมส่งพารามิเตอร์เข้าไป 5 ตัว เขียนไว้เป็นฟังก์ชั่นเวลาเรียกใช้งานจะได้สะดวก

$param_google_username – username ที่ใช้ล้อกอินเข้ากูเกิล

$param_google_password – password ที่ใช้ล้อกอินเข้ากูเกิล

$param_title – หัวข้อรายการ

$param_content – เนื้อหาที่เราจะให้ส่ง sms หาเรา

 

 

5.ส่ง SMS

87
88
89
90
91
92
93
94
95
<?php
$google_username = "xxxxxxxxxxx"; // username เข้ากูเกิลของคุณ
$google_password = "xxxxxxxxxxx"; // password เข้ากูเกิลของคุณ
$title = "เรื่องนี้ต้องขยาย";
$content = "เนื่องจากนางสมศรีแอบรักกับนายสมชาย โดยพ่อสมปองไม่ทราบ"; 
 
// ส่ง SMS
send_sms( $google_username , $google_password , $title ,$content );
?>

 

6.Final โค้ดทั้งหมด จะเป็นดังนี้

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
93
94
95
96
<?php
function send_sms( $param_google_username , $param_google_password,$param_title,$param_content ){
 
	/**
	 * @Include Zend_Loader
	 */
	require_once 'Zend/Loader.php';
 
	/**
	 * @Load Zend_Gdata
	 */
	Zend_Loader::loadClass('Zend_Gdata');
 
	/**
	 * @Load Zend_Gdata_AuthSub
	 */
	Zend_Loader::loadClass('Zend_Gdata_AuthSub');
 
	/**
	 * @Load Zend_Gdata_ClientLogin
	 */
	Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
 
	/**
	 * @Load Zend_Gdata_HttpClient
	 */
	Zend_Loader::loadClass('Zend_Gdata_HttpClient');
 
	/**
	 * @Load Zend_Gdata_Calendar
	 */
	Zend_Loader::loadClass('Zend_Gdata_Calendar');
 
	// Parameters for ClientAuth authentication
	$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
	$user = $param_google_username ;
	$pass =$param_google_password ;
 
	// Create an authenticated HTTP client
	$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
 
	// Create an instance of the Calendar service
	$service = new Zend_Gdata_Calendar($client);
 
	// Create a new entry using the calendar service's magic factory method
	$event= $service->newEventEntry();
 
	// Populate the event with the desired information
	// Note that each attribute is crated as an instance of a matching class
	$event->title = $service->newTitle( $param_title );
 
	//$event->where = array($service->newWhere("Mountain View, California"));
	$event->content = $service->newContent( $param_content );
 
	// Set the date using RFC 3339 format.
	$startDate = date( "Y-m-d" );//"2009-01-15";
	$startTime = date( "H:i" , strtotime("+3 minutes") );
	$endDate = date( "Y-m-d" );
	$endTime = date( "H:i" , strtotime("+6 minutes") );
	$tzOffset = "+07";
 
	$when = $service->newWhen();
	$when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
	$when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
 
	// Create a new reminder object. It should be set to send an email
	// to the user 10 minutes beforehand.
	$reminder = $service->newReminder();
	$reminder->method = "sms";
	$reminder->minutes = "2";
 
	$when->reminders = array($reminder);
 
	$event->when = array($when);
 
	// Upload the event to the calendar server
	// A copy of the event as it is recorded on the server is returned
	if ($newEvent = $service->insertEvent($event)){
 
		return true ;
	}else{
 
		return false;
	}
 
}
 
$google_username = "xxxxxxxxxxx"; // username เข้ากูเกิลของคุณ
$google_password = "xxxxxxxxxxx"; // password เข้ากูเกิลของคุณ
$title = "เรื่องนี้ต้องขยาย";
$content = "เนื่องจากนางสมศรีแอบรักกับนายสมชาย โดยพ่อสมปองไม่ทราบ"; 
 
// ส่ง SMS
send_sms( $google_username , $google_password , $title ,$content );
 
?>

 

ส่งท้าย

ความสำคัญทั้งหมดจะอยู่ที่ ฟังก์ชั่น send_sms ถ้าคุณจะทดสอบโค้ดชุดนี้ในเครื่องของคุณ ต้องมั่นใจนะครับว่า คุณได้เปิด OpenSSL ใน php.ini ไว้เรียบร้อย

 

โค้ดชุดนี้สามารถเอาไปประยุกต์ เป็นระบบส่ง SMS ภายในองค์กร หรือในกลุ่มเพื่อน สาเหตุที่บอกว่าประยุกต์ใช้เป็นกลุ่มอย่างนี้ เพราะว่าคุณต้องรู้ username กับ password google ของเขา จึงสามารถไปสร้างรายการแจ้งล่วงหน้าด้วย SMS ใน google calendar ของเขาได้

 

อ้างอิง : การใช้งาน google calendar API อย่างละเอียด

**** ย้ำเตือนนะครับ ****

  1. ต้องใช้ UTF-8 เท่านั้น
  2. ส่วนเรื่องเวลา จะขึ้นอยู่กับว่าโฮสต์ของท่านตั้งอยู่ประเทศไหน ถ้าไม่ได้อยู่ในไทย ก็แก้ตรงนี้ $tzOffset = “+07”;