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 มาใช้


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 อีกที ดังนี้

$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

$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


$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"]."
";
echo "story: ".$res["story"]."
";
echo "homepage : ".$res["homepage"]."
";
echo "image : ";
}


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

Comments

Popular posts from this blog

Artillery Hornet + OctoPrint + Klipper

ย้ายบ้าน

ใต้ร่มกาสาวพัตร