February 2009
You are currently browsing the articles from KomKid.Net written in the month of February 2009.
มาต่อกันด้วยการส่งตรง Message ถึงเพื่อนที่เราต้องการ
URL: http://api.noknok.sanook.com/rest/direct_message_new
Format: xml
Method(s): POST
Parameters:
* authToken (required:GET)
* message (required:POST)
* message_to (required:POST)
Return:
* Sending status (true or false)
ก็เอา code เดิมมาแก้ไข form นิดหน่อย
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $base = 'http://api.noknok.sanook.com/rest/auth';
$query_string = 'username=komkid&password=******';
$url = "$base?$query_string";
$xml = file_get_contents($url);
$dom = new DOMDocument ('1.0', 'UTF-8');
if ($dom->loadXML($xml) === false) {
die('Parsing failed');
}
$res = xml_to_result ($dom);
if($res["stat"] == "ok"){
$query_string = 'authToken='.$res["authToken"];
$str= " Send Direct Message <br>";
$str.= "<form method=\"post\" action=\"http://api.noknok.sanook.com/rest/direct_message_new?$query_string\">";
$str.= "message : <input type=\"text\" name=\"message\"><br>";
$str.= "send to : <input type=\"text\" name=\"message_to\"><br>";
$str.= "<input type=\"submit\" value=\"send\">";
$str.= "</form>";
echo $str;
} |
ได้ form ดังรูป

แล้วก็ได้ผลดังนี้

Written by Komkid on February 24th, 2009 with no comments.
Read more articles on Internet and Programming.
มาต่อกันด้วย การ Update Status ของเราด้วย
new
URL: http://api.noknok.sanook.com/rest/message_new
Format: XML
Method(s): GET, POST
Parameters:
* authToken (required:GET)
* message (required:POST)
Return
* Sending message data
(ผ่านไปไม่กี่วัน URL เปลี่ยนซะแล้ว จาก http://api.noknok.in.th/rest/ เป็น http://api.noknok.sanook.com/rest)
ซึ่งคราวนี้จะต้องส่งแบบ POST เพราะฉะันั้นก็ต้องสร้าง FORM ไว้ส่ง
ก็เอาแบบง่าย ๆ นะครับ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| $base = 'http://api.noknok.sanook.com/rest/auth';
$query_string = 'username=komkid&password=******';
$url = $base.'?'.$query_string;
$xml = file_get_contents($url);
$dom = new DOMDocument ('1.0', 'UTF-8');
if ($dom->loadXML($xml) === false) {
die('Parsing failed');
}
$res = xml_to_result ($dom);
if($res[stat] == 'ok'){
$query_string = 'authToken='.$res[authToken ];
$str=<<<FORM
Update Status <br >
<form method ="post" action ="http://api.noknok.sanook.com/rest/message_new?$query_string">
message : <input type ="text" name ="message"><br >
<input type ="submit" value ="update">
</form >
FORM ;
echo $str;
} |

ได้ผลดังรูป

อ้อ อย่าลืมตั้งค่า Encoding เป็น UTF8 ด้วยนะครับ
Introduction
API Protocol
* Representational_State_Transfer(REST)
* HTTP Requests: Post and GET
Data Format
* XML, *Json, *RSS,*ATOM
Encoding
* UTF8
Written by Komkid on February 23rd, 2009 with no comments.
Read more articles on Internet and Programming.
มาต่อกันด้วย Profile ครับท่าน
URL: http://api.noknok.in.th/rest/profile
Format: xml
Method(s): GET
Parameters:
* authToken (required)
Return:
* User’s profile data
เริ่มด้วยการลองคลิกเข้าไปแบบตอนที่ Authen แต่คราวนี้กลับได้ผลดังรูป

แสดงว่าต้องเริ่มสื่อสารกันเป็นจริงเป็นจังแล้วล่ะ
ตามคู่มือที่เค้าบอกก็คือ format ของการสื่อสารกัน ก็คือ xml
ดังนั้นก็เลยต้องเลือกวิธีที่จะใช้ ซึ่งผมเลือกใช้ DOM ก็จัดการหา function มาใช้งานสำหรับดึงค่าจาก xml มาใช้
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
| function xml_to_result ($dom) {
$root = $dom->firstChild;
foreach($root->attributes as $attr) $res[$attr->name] = $attr->value;
$node = $root->firstChild;
$i = 0;
while($node) {
switch($node->nodeName) {
case 'Result':
$subnode = $node->firstChild;
while($subnode) {
$subnodes = $subnode->childNodes;
foreach($subnodes as $n) {
if($n->hasChildNodes()) {
foreach($n->childNodes as $cn) $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
} else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
}
$subnode = $subnode->nextSibling;
}
break;
default:
$res[$node->nodeName] = trim($node->nodeValue);
$i--;
break;
}
$i++;
$node = $node->nextSibling;
}
return $res;
} |
จากนั้นก็ทำการ authen ก่อน เพราะเจ้า profile นี่มันต้องการ authToken ซึ่งจะได้มาจากเจ้า auth อีกที ดังนี้
1 2 3 4 5 6 7
| $base = 'http://api.noknok.in.th/rest/auth';
$query_string = 'username=komkid&password=******';
$url = "$base?$query_string";
$xml = file_get_contents($url);
echo $xml; |
ซึ่งก็ได้ผลดังนี้

แล้วก็ตามด้วยการขอดู profile ด้วย code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $base = 'http://api.noknok.in.th/rest/auth';
$query_string = 'username=komkid&password=******';
$url = "$base?$query_string";
$xml = file_get_contents($url);
$dom = new DOMDocument ('1.0', 'UTF-8');
if ($dom->loadXML($xml) === false) {
die('Parsing failed');
}
$res = xml_to_result ($dom);
if($res["stat"] == "ok"){
$base = 'http://api.noknok.in.th/rest/profile';
$query_string = 'authToken='.$res["authToken"];
$url = "$base?$query_string";
$xml = file_get_contents($url);
echo $xml;
} |
ซึ่งก็ได้ผลดังรูปครับ

หรือจะจัดรูปแบบในการแสดงผลซะหน่อยด้วย code
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
| $base = 'http://api.noknok.in.th/rest/auth';
$query_string = 'username=komkid&password=******';
$url = "$base?$query_string";
$xml = file_get_contents($url);
$dom = new DOMDocument ('1.0', 'UTF-8');
if ($dom->loadXML($xml) === false) {
die('Parsing failed');
}
$res = xml_to_result ($dom);
if($res["stat"] == "ok"){
$base = 'http://api.noknok.in.th/rest/profile';
$query_string = 'authToken='.$res["authToken"];
$url = "$base?$query_string";
$xml = file_get_contents($url);
$dom = new DOMDocument ('1.0', 'UTF-8');
if ($dom->loadXML($xml) === false) {
die('Parsing failed');
}
$res = xml_to_result ($dom);
echo "name : ".$res["name"]."<br>";
echo "story: ".$res["story"]."<br>";
echo "homepage : ".$res["homepage"]."<br>";
echo "image : <img src=".$res["profile_image_url"].">";
} |
ก็ได้ผลดังรูป

Written by Komkid on February 19th, 2009 with no comments.
Read more articles on Internet and Programming.
คราวนี้มาลองใช้งาน API ของ noknok.in.th กันบ้าง
เริ่มด้วยสิ่งที่จำเป็นต้องใช้ในครั้งนี้ (เฉพาะวิธีของผม) ก็คือ
1.Account ของ noknok.in.th
2.Server ที่ใช้งาน PHP และ DOM ได้ (ความจริงไม่ต้อง DOM ก็ได้ครับ แต่บังเอิญผมเริ่มด้วยเจ้านี่ ก็ขอแสดงด้วยเจ้า DOM นี่แหล่ะครับ)
เริ่มกันเลยนะครับ ตามที่ noknok.in.th เข้ามีคำแนะนำให้ที่ http://en.wikipedia.org/wiki/User:Noknokdev (API ของเค้าให้ใช้ผ่าน REST protocol ซึ่งมี link ให้ไปศึกษากันเองอยู่แล้ว)
อันดับแรกก็ลองงานกลุ่มของ Account Methods เริ่มด้วย
Authen
URL: http://api.noknok.in.th/rest/auth
Format: xml
Method(s): GET, POST
Parameters:
* username (required)
* password (required)
Return:
* authToken
ลองคลิกเข้าไปดูก็จะได้ popup สำหรับ login

และถ้าป้อน username และ password ถูกต้องก็จะได้ผล ดังรูป

Written by Komkid on February 18th, 2009 with no comments.
Read more articles on Internet and Programming.
micro blogging ที่ดังที่สุดตอนนี้ก็คงเป็น twitter แต่ในไทยเองก็มี noknok ที่หลายคนบอกว่าเป็น twitter clone
ประโยชน์หรือไอเดียในการใช้งาน ก็
1.Follow กันเองกับแฟน ใช้ส่ง sms หากันฟรี
2.ทำในลักษณะ groupmail สมัครแล้วก็แจก password กัน แล้วก็ update ข่าวสารกันทั้งก๊วน
3.โปรโมทเว็บ update web ทีก็ส่งข่าวสารให้พวกสาวกได้รับรู้
4.ฯลฯ ใครมีไอเดียในการใช้งานอื่น ๆ ก็บอกกันบ้างเด้อ
วิธีสมัครก็ง่ายแสนง่าย ตั้ง username > เลือกรหัสผ่าน > ใส่ email
จากนั้นก็เลือกผู้ที่เราจะติดตามข่าวสาร เลือกเอาว่าจะรับข้อความทางโทรศัพท์มั้ย
ขั้นตอนก็ง่าย ๆ คงไม่ต้องอธิบาย (เด็ก ๆ ก็ทำเป็น)
เอาไว้มาดูกันที่ตอนต่อไปเรื่องการใช้งาน API ดีกว่า
Written by Komkid on February 18th, 2009 with no comments.
Read more articles on Internet and Programming.
« Older articles
No newer articles