ปตท web service
วันๆ ว่างๆ ไม่รู้จะทำอะไร เหลือบมองเห็นช่องราคาน้ำมันจากหน้าเว็บเลย เข้าไปดู กลายเป็น Iframe ซึ่งไม่ค่อยชอบเท่าใหร่ เลยไปดูว่าเอาข้อมูลมาจาใหน ที่แท้เป็น frame ที่ได้จากเว็บของ ปตทเลยนี่เอง เลื่อนลงมาล่างๆ หน่อยมี web service ให้ยริการด้วย เลยแก้ว่างด้วยการเขียน web service เรียกราคาน้ำมันซะเลย
ในการเขียนครั้งนี้ก็ไม่ได้ เวอร์ หลูหลา สะแมนแตน อะไรมากมาย แค่PHP+ lib ของ nusoup ตัวเดียวเท่านั้น ถ้าไม่มี lib ตัวนี้อยู่ก็เข้า google แล้วหาคำว่า nosoap รับลองเจอ
php เองจะว่าว่าเป็นของเด็กๆ ก็ทำอะไรที่ Enterprise กับเข้าได้เหมือนกันนะ ที่เห็นว่าเป็นเด็กๆ หรือ Basic เพราะอะไรหลายๆ อย่างไม่บอกดีกว่า มาเขียนกันดีกว่า
เริ่มแรกก็ เข้าไปที่ http://www.pttplc.com/th/nc_oi.aspx ซะก่อน บอกแล้วว่าล่างๆ จะบอกว่า web service ด้วย และก็มีมาให้ครบเลยทั้ง WSDL และเอกสารด้วย อ่านกันได้ตามสบาย
ทีนี้เรารมาดูวิธีการเขียนด้วย nusoap กันดีกว่า
require_once('./lib/nusoap.php');
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
$client = new nusoap_client('http://www.pttplc.com/pttinfo.asmx?wsdl',</code>
<code> 'wsdl', $proxyhost, $proxyport, $proxyusername, $proxypassword);
$err = $client->getError();
if ($err) {
echo '
</code>
<h2>Constructor error</h2>
<pre>' . $err . '</pre>
';
}
$operation = 'CurrentOilPrice';
$client->decodeUTF8(false);
$result = $client->call(
$operation,
array('Language' => 'TH')
);
// Check for a fault
if ($client->fault) {
echo '
<h2>Fault</h2>
<pre>';
print_r($result);
echo '</pre>
';
exit();
}
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '
<h2>Error</h2>
<pre>' . $err . '</pre>
';
exit();
}
$result = $result['CurrentOilPriceResult'];
$content = new xx_xml($result, 'contents');
$PRICE_DATE = $content->data['PTT_DS|Table|PRICE_DATE']['data'];
$PRODUCT = $content->data['PTT_DS|Table|PRODUCT']['data'];
$PRICE = $content->data['PTT_DS|Table|PRICE']['data'];
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style> table{ width:350px; border:1px solid #999999; }
table th{ background-color:#3f3f3f; color:#FFFFFF; }
.hl{ background-color:#EFEFEF; }
</style>
<table>
<tr>
<th>Type</th>
<th>Price</th>
<th>Update date</th>
</tr>
<tr>
<td></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</table>
class xx_xml {
// XML parser variables
var $parser;
var $name;
var $attr;
var $data = array();
var $stack = array();
var $keys;
var $path;
// either you pass url atau contents.
// Use 'url' or 'contents' for the parameter
var $type;
// function with the default parameter value
function xx_xml($url='http://www.opocot.com', $type='url') {
$this->type = $type;
$this->url = $url;
$this->parse();
}
// parse XML data
function parse()
{
$data = '';
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'startXML', 'endXML');
xml_set_character_data_handler($this->parser, 'charXML');
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
if ($this->type == 'url') {
// if use type = 'url' now we open the XML with fopen
if (!($fp = @fopen($this->url, 'rb'))) {
$this->error("Cannot open {$this->url}");
}
while (($data = fread($fp, 8192))) {
if (!xml_parse($this->parser, $data, feof($fp))) {
$this->error(sprintf('XML error at line %d column %d',
xml_get_current_line_number($this->parser),
xml_get_current_column_number($this->parser)));
}
}
} else if ($this->type == 'contents') {
// Now we can pass the contents, maybe if you want
// to use CURL, SOCK or other method.
$lines = explode("\n",$this->url);
foreach ($lines as $val) {
if (trim($val) == '')
continue;
$data = $val . "\n";
if (!xml_parse($this->parser, $data)) {
$this->error(sprintf('XML error at line %d column %d',
xml_get_current_line_number($this->parser),
xml_get_current_column_number($this->parser)));
}
}
}
}
function startXML($parser, $name, $attr) {
$this->stack[$name] = array();
$keys = '';
$total = count($this->stack)-1;
$i=0;
foreach ($this->stack as $key => $val) {
if (count($this->stack) > 1) {
if ($total == $i)
$keys .= $key;
else
$keys .= $key . '|'; // The saparator
}
else
$keys .= $key;
$i++;
}
if (array_key_exists($keys, $this->data)) {
$this->data[$keys][] = $attr;
} else
$this->data[$keys] = $attr;
$this->keys = $keys;
}
function endXML($parser, $name) {
end($this->stack);
if (key($this->stack) == $name)
array_pop($this->stack);
}
function charXML($parser, $data) {
if (trim($data) != '')
$this->data[$this->keys]['data'][] = trim(str_replace("\n", '', $data));
}
function error($msg) {
echo "
<p align="\"> <font color="#00ed00"><strong>Error: $msg</strong></font></p>
";
exit();
}
}
?>
อันนี้ที่เห็นเป็น Code คร่าวๆ นะ จะได้หน้าตาแบบ หน้านี้ ครับ ไปที่นี่เลย http://www.meteenee.com/pttservice/index.php
จะว่าไปแล้วก็ขอบคุณ ปตท. นะ เพราะว่า บ้านเรานี้ไม่ค่อยมี service ให้ใช้เลย เฮ้อ รอต่อไป รอหน่วยงานที่เห็นถึงความสำคัญ
