โค้ด: เลือกทั้งหมด
print'<hr> JSON </hr>';
$json_value = '{
"name": "mindphp/m_button_like",
"type": "phpbb-extension",
"description": "Button Like",
"homepage": "https://www.mindphp.com",
"version": "1.1.1",
"authors": [
{
"name": "mindphp",
"homepage": "https://www.mindphp.com",
"role": "Developer"
}
]
}';
$json_value = json_decode($json_value);
print '<pre>';
var_dump($json_value);
print '</pre>';
print 'version:' . $json_value->version;
JSON
object(stdClass)#1 (6) {
["name"]=>
string(21) "mindphp/m_button_like"
["type"]=>
string(15) "phpbb-extension"
["description"]=>
string(11) "Button Like"
["homepage"]=>
string(22) "https://www.mindphp.com"
["version"]=>
string(5) "1.1.1"
["authors"]=>
array(1) {
[0]=>
object(stdClass)#2 (3) {
["name"]=>
string(7) "mindphp"
["homepage"]=>
string(22) "https://www.mindphp.com"
["role"]=>
string(9) "Developer"
}
}
}
โค้ด: เลือกทั้งหมด
print 'version:' . $json_value->version;
version:1.1.1
โค้ด: เลือกทั้งหมด
print'<hr> XML </hr>';
$xml_value = '<xml_value>
<name> mindphp/m_button_like </name>
<type>phpbb-extension</type>
<description>Button Like</description>
<homepage>https://www.mindphp.com</homepage>
<version>1.1.1</version>
<authors>
<name>mindphp</name>
<homepage>mindphp</homepage>
<role>Developer</role>
</authors>
</xml_value>';
$result_xml = simplexml_load_string($xml_value);
print '<pre>';
var_dump($result_xml);
print '</pre>';
print 'homepage:' . $result_xml->authors->homepage;
XML
object(SimpleXMLElement)#3 (6) {
["name"]=>
string(23) " mindphp/m_button_like "
["type"]=>
string(15) "phpbb-extension"
["description"]=>
string(11) "Button Like"
["homepage"]=>
string(22) "https://www.mindphp.com"
["version"]=>
string(5) "1.1.1"
["authors"]=>
object(SimpleXMLElement)#4 (3) {
["name"]=>
string(7) "mindphp"
["homepage"]=>
string(7) "mindphp"
["role"]=>
string(9) "Developer"
}
}
โค้ด: เลือกทั้งหมด
print 'homepage:' . $result_xml->authors->homepage;
homepage:mindphp
โค้ด: เลือกทั้งหมด
print'<hr> YML </hr>';
$yml_value = '
name : "mindphp/m_button_like"
type : "phpbb-extension"
description: "Button Like"
homepage: "https://www.mindphp.com"
version : "1.1.1"
authors:
name: "mindphp"
homepage: "https://www.mindphp.com"
role: "Developer"
';
require_once 'Spyc.php';
$result_yml = Spyc::YAMLLoad($yml_value);
$result_yml = json_decode(json_encode($result_yml),FALSE);
print '<pre>';
var_dump($result_yml);
print '</pre>';
print 'name:' . $result_yml->authors->name;
YML
object(stdClass)#6 (6) {
["name"]=>
string(21) "mindphp/m_button_like"
["type"]=>
string(15) "phpbb-extension"
["description"]=>
string(11) "Button Like"
["homepage"]=>
string(22) "https://www.mindphp.com"
["version"]=>
string(5) "1.1.1"
["authors"]=>
object(stdClass)#4 (3) {
["name"]=>
string(7) "mindphp"
["homepage"]=>
string(22) "https://www.mindphp.com"
["role"]=>
string(9) "Developer"
}
}
โค้ด: เลือกทั้งหมด
'name:' . $result_yml->authors->name;
** ส่วนของ YML จะต้องใช้งาน YAML library มีให้ใช้ด้วยกันหลายภาษา ส่วนภาษา PHP ที่นำมายกตัวอย่าง จะใช้ SYPCname:mindphp
ดาวน์โหลดได้ที่ https://github.com/mustangostang/spyc/
จากนั้นให้ทำการ
โค้ด: เลือกทั้งหมด
require_once 'Spyc.php';
เพื่อใช้งาน