Amazon For Developers [ตอนที่ 3] basic PHP+XML

เริ่มโดย zenithsoul, 19 ธันวาคม 2008, 22:52:53

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

zenithsoul

สวัสดีครับชาวเสียวบอร์ดทุกคนผมกลับมาแล้ว  ;D (บางคนอาจจะรอจนเงือกแห้ง  :-X )

วันนี้ผมแนะนำการใช้งานเกี่ยวกับ Basic PHP + XML ซึ่งเป็นประโยชน์ต่อการเขียน WebAPI กับ Amazon นะครับ

คือต้องมีพื้นฐานเกี่ยวกับการเขียน php มาบ้างแล้วนะครับ ให้ศึกษาเอง

เตรียมเครื่องมือกันก่อนครับ

AppServ 2.5.10
อ้างถึง
http://www.appservnetwork.com/

วิธีติดตั้ง Search หาGoogleเอานะครับ

code ตัวอย่างชื่อ example.php
อ้างถึง
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
       <movie>
                   <title>PHP: Behind the Parser</title>
                   <characters>
                          <character>
                              <name>Ms. Coder</name>
                              <actor>Onlivia Actora</actor>
                         </character>
                          <character>
                              <name>Mr. Coder</name>
                              <actor>El Act&#211;r</actor>
                          </character>
                   </characters>
                   <plot>
                       So, this language. It's like, a programming language. Or is it a
                       scripting language? All is revealed in this thrilling horror spoof
                       of a documentary.
                  </plot>
                  <great-lines>
                        <line>PHP solves all my web problems</line>
                 </great-lines>
                 <rating type="thumbs">7</rating>
                 <rating type="stars">5</rating>
        </movie>
</movies>
XML;
?>


สังเกตจาก tag xml นะครับผมเรียงให้แล้ว

ตัวอย่างที่  1.การดึงค่าใน  tab  <plot>  .........  </plot>

ไฟล์ get1.php
อ้างถึง
<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie[0]->plot; //?>

งงละซิว่าตัวแปรแต่ละตัวมันหมายความว่ายังไง

include 'example.php';   // บรรทัดนี้ดึงไฟล์ example.php เข้ามาด้วยแต่ต้องอยู่ในโฟเดอร์เดียวกันนะ
$xml = new SimpleXMLElement($xmlstr); // ดึงข้อมูลจากตัวแปร $xmlstr ที่อยู่ในไฟล์ example.php
                                                             เข้าสู่ คลาสของ SimpleXMLElement เริ่มการอ่าน tag
                                                             แต่ละ tag ไว้ในตัวแปร $xml

echo $xml->movie[0]->plot;  // echo <- เป็นฟังชั่นการแสดงค่า  $xml->movie[0]->plot ออกมา

ลองรันไฟล์ดูนะครับ +++++++++++++++ จะเห็นผลเป็น

อ้างถึง
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.

movie[0]  ตรงนี้ที่เป็น 0 เนื่องจาก tag movie มีแค่อันเดียวครับ (ถ้าใครไม่เข้าใจกรุณาอ่านเกี่ยวกับ Array ใน PHP)

ดังตารางข้างล่างครับซึ่งมีอยู่ Tag เดียว

อ้างถึง
       <movie>
             ...................................................
        </movie>

ไฟล์ get2.php ดึงค่าตัวแปร์ ใน Tag line
อ้างถึง
<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->movie->{'great-lines'}->line; // "PHP solves all my web problems"
?>

สังเกตว่าไฟล์นี้การดึงค่าออกมาแปลกไปนะครับเนื่องจากใน tag great-lines มีอักขระพิเศษซึ่งไม่ใช่ตัวอักษรครับ

zenithsoul

#1
ไฟล์ get4.php
อ้างถึง
<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->movie as $movie) {
   echo $movie->plot, '<br />';
}
?>

อันนี้เป็นการดึงค่าทุก ๆ tag plot ออกมาแสดงครับถ้ามี tag plot อยู่เป็น 3 มันก็แสดงออกมาหมดเลยครับ

ตัวอย่างข้างล่าง
อ้างถึง
........................
.........
<plot>
    aasdadsadasdasdsa
</plot>
<plot>
    123456456546546
</plot>
<plot>
    awdq54wqqwe4q6w5d1
</plot>
........
......................

ก็จะแสดงเป็น
อ้างถึงaasdadsadasdasdsa123456456546546awdq54wqqwe4q6w5d1

ไฟล์ get5.php
อ้างถึง<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->movie[0]->rating as $rating) {

    switch((string) $rating['type']) {
    case 'thumbs':
        echo $rating, ' thumbs up';
        break;
    case 'stars':
        echo $rating, ' stars';
        break;
    }

}
?>

เราเห็น tag ข้างล่างนี้ไหมครับ
อ้างถึง
                <rating type="thumbs">7</rating>
                <rating type="stars">5</rating>
ใน tag นี้ยังมีค่าในตัวแปรใน tag rating คือตัวแปร type ซึ่งเรียกว่า attributes ครับ ซึ่งในไฟล์ get5.php

นี้จะเป็นการดึงค่าใน tag rating โดยทำการใช้ swich เป็นตัวแยกเงื่อนไงโดยเข้าถึง (string) $rating['type']

โดยที่ $xml->movie[0]->rating as $rating หรืออ่านว่า $xml->movie[0]->rating มีค่าเท่ากับ $rating

หรือเราจะใช้ $xml->movie[0]->rating['type']  ก็ได้ครับ


ตัวอย่างสุดท้ายการค้นหา tag ไฟล์ get6.php
อ้างถึง
<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->xpath('//character') as $character) {
    echo $character->name, 'played by ', $character->actor, '<br />';
}
?>

จากไฟล์ข้างบนเนี่ยจะเห็นว่าเราใช้ xpath เลยโดยไม่มีการอ้างอิงเข้าไปในตัวแปรเลย และ สังเกตดูดี ๆ นะครับว่ามี

'  //  ' อยู่สองครั้งนั่นหมายความว่า

อ้างถึง
<movie>                                                                 ไม่ใส่  /
                   <characters>                                           ใส่  /   ครั้งที่ 1
                          <character>                                      ใส่ /    ครั้งที่ 2
                              <name>Ms. Coder</name>
                              <actor>Onlivia Actora</actor>
                         </character>
                          <character>
                              <name>Mr. Coder</name>
                              <actor>El Act&#211;r</actor>
                          </character>
                   </characters>
        </movie>

ถ้าเอา / ออกตัวหนึ่งมันก็จะไม่แสดงค่าครับ แสดงว่าเวลาถ้าเราอ้างอิงเข้าสู่ tag แบบ Search เอาต้องกำหนด

path  ให้กับ Xpath ด้วยครับ

zenithsoul

#2
หมดแล้วครับ ยังไงทั้งหมดทั่งมวลนี้เนี่ย ต้องนำไปใช้ในการเขียนโปรแกรมกับ API ของ Amazon นะครับ

ซึ่งส่วนมากผมมองว่าน่าจะเป็นการอ่านข้อมูลมาจากทาง Amazon อย่างเดียวครับ

ตอนต่อไป ตอนที่ 4 ลุยงานจริง ครั้งที่ 1 กับ Amazon API ครับ

ปล. ผมเห็นแว๊บ ๆ ว่า shopping ก็มี API เหมือนกับ Amazon อยู่นะครับ
ปล. ถูกใจให้     +1    นะครับตามศรัทธา

crongcrang

[direct=https://webapp.jeab.site]Submit Webapp[/direct]


john007

[direct=http://www.aiyaadressy.com]เสื้อผ้าเกาหลีราคาถูก[/direct] | [direct=http://www.itrendy24.com]สินค้าดี ราคาถูก[/direct]

beername

เดียวไปหัดเขียนphpก่อน  :P

inDexTop10

:)

เยียมครับ

จะรอ ของจริง

:)
[direct=http://goo.gl/h2PZom]Digitalocean.com[/direct] - ถูกและดีจริง ลด 10$ ลงไปเป็นบอก

ohmohm

#8
ตามมาดูต่อ ขอบคุณครับ
   
Amazon For Developers [ตอนที่ 2] เตรียมเครื่องมือก่อน
http://www.thaiseoboard.com/index.php/topic,46149.0.html

Amazon For Developers [ตอนที่ 4] ลงมือกันดีกว่าครับ Amazon API + ItemLookup
http://www.thaiseoboard.com/index.php/topic,47531.0.html

AlphaCentury

ติดตามอยู่นะครับ เคยเล่นแต่ CMS กะเว็ปปั่น(AOM)

มีคนสอนภาษาไทยงี้จะได้เล่น API ของ Amazon เป็นซะที (เคยเข้าไปอ่านเห็นภาษาอังกฤษ แล้วตาลาย เลิกเลย อิอิ  :P)

homekung

[direct=https://animateus.in.th]รับทำ 2d animation[/direct] [direct=https://animateus.in.th]รับทำ 2d infographics[/direct][direct=https://www.facebook.com/animateus122]รับทำ 2d cartoon animation[/direct][direct=https://www.fiverr.com/chanarachl/make-2d-animation-or-whiteboard-video-for-any-purpose]2d animation[/direct][direct=https://vimeo.com/animateus10]รับทำ animation[/direct]

atmake



natee248

ต้องเป็นเทพแน่ๆ   มนุษย์ธรรมดาไม่เข้าใจอ่ะ  :-X

fredomlife

เปิดไซต์แล้วครับ (อย่างไม่เป็นทางการ) fredomlife.com

ggmakerich

###KAOHOLDING CO.,LTD.###

# จัดหาและจัดจำหน่ายสินค้าอุปโภคบริโภคเพื่อการส่งออกต่างประเทศ เช่น ลาว พม่า เวียดนาม กัมพูชา มาเลเซีย และจีน รวมทั้งประเทศอื่น

สนใจติดต่อ line id/tel : 0911579898

khanna


gamaxx

มาถึงตรงนี้ขอพักแป๊บ...อิอิอิไปศึกษา PHP ก่อน :P..คงอีกนานแน่กว่าจะได้ใช้..
แต่เอาไปใช้กับ Plug in ของ WP ได้ครับไปลองเล่นมาเจ๋งดี...... ;)