February 19th, 2009

You are currently browsing the articles from KomKid.Net written on February 19th, 2009.

noknok.in.th API #03 Profile

มาต่อกันด้วย Profile ครับท่าน

URL: http://api.noknok.in.th/rest/profile
Format: xml
Method(s): GET
Parameters:
* authToken (required)
Return:
* User’s profile data

เริ่มด้วยการลองคลิกเข้าไปแบบตอนที่ Authen แต่คราวนี้กลับได้ผลดังรูป
noknok_api_profile_fail

แสดงว่าต้องเริ่มสื่อสารกันเป็นจริงเป็นจังแล้วล่ะ
ตามคู่มือที่เค้าบอกก็คือ 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;

ซึ่งก็ได้ผลดังนี้
noknok_api_profile_ok

แล้วก็ตามด้วยการขอดู 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;
}

ซึ่งก็ได้ผลดังรูปครับ
noknok_api_profile_result

หรือจะจัดรูปแบบในการแสดงผลซะหน่อยด้วย 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"].">";
}

ก็ได้ผลดังรูป
noknok_api_profile_show

Written by Komkid on February 19th, 2009 with no comments.
Read more articles on Internet and Programming.