Site icon บริษัท โค๊ดบี จำกัด

สอน codeigniter ดึงรูปทั้งหมดจากหน้าเว็บด้วย simple_html_dom

php-codeigniter-framework

php-codeigniter-framework

<h3>สอน codeigniter ดึงรูปทั้งหมดจากหน้าเว็บด้วย simple&lowbar;html&lowbar;dom<&sol;h3>&NewLine;<p><&sol;br><br &sol;>&NewLine;<strong>สอน codeigniter ดึงรูปทั้งหมดจากหน้าเว็บด้วย simple&lowbar;html&lowbar;dom<&sol;strong><br &sol;>&NewLine;simple&lowbar;html&lowbar;dom เป็น php Class ที่ถูกพัฒนาขึ้นมาสำหรับการดึงข้อมูลจากหน้าเว็บและสามารถเข้าถึง Element ข้อมูลในหน้านั้น ๆ ทั้ง div class&comma; id&comma; attribute นั้นทำให้เราสามารถรู้ว่าแท็ก img แอททริบิวต์ src มีค่าและแสดงรูปอะไร <&excl;--more--><&sol;p>&NewLine;<p>เราสามารถนำ simple&lowbar;html&lowbar;dom มาใช้งานร่วมกับ codeigniter ในลักษณะ third party ได้ ลองมาดูกันครับว่า จะสามารถใช้งานได้อย่างไร<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>ประโยชน์ของคลาส simple&lowbar;html&lowbar;dom<&sol;h4>&NewLine;<p>&&num;8211&semi; ใช้โหลดข้อมูลจากหน้าเว็บไซต์ได้ทั้งหน้า<br &sol;>&NewLine;&&num;8211&semi; สามารถเข้าถึงข้อมูลต่าง ๆ ในหน้าเว็บง่าย ๆ ในรูปแบบ Array Object<br &sol;>&NewLine;&&num;8211&semi; เร็ว<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>ดาวน์โหลดและติดตั้ง simple&lowbar;html&lowbar;dom&period;php<&sol;h4>&NewLine;<p>1&period; ดาวน์โหลด <a href&equals;"http&colon;&sol;&sol;sourceforge&period;net&sol;projects&sol;simplehtmldom&sol;" target&equals;"&lowbar;blank">simple&lowbar;html&lowbar;dom<&sol;a> คลาส unzip และวางไว้ที่โฟลเด้อ application&sol;third&lowbar;party<br &sol;>&NewLine;2&period; สร้าง Controller ตั้งชื่อว่า HtmlController&period;php ในส่วนของ Construct ให้โหลดคลาสมาใช้งานดังนี้<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >class HtmlController extends CI&lowbar;Controller &lbrace; &NewLine; &NewLine; function &lowbar;&lowbar;construct&lpar;&rpar; &lbrace; &NewLine; parent&colon;&colon;&lowbar;&lowbar;construct&lpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; โหลด simple&lowbar;html&lowbar;dom &NewLine; include APPPATH &period; 'third&lowbar;party&sol;simple&lowbar;html&lowbar;dom&period;php'&semi; &NewLine; &rcub;<&sol;pre>&NewLine;<p>3&period; ในส่วนของ index ให้ประกาศใช้งาน simple&lowbar;html&lowbar;dom คลาสดังนี้<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;&quest;php &NewLine; &NewLine;class HtmlController extends CI&lowbar;Controller &lbrace; &NewLine; &NewLine; function &lowbar;&lowbar;construct&lpar;&rpar; &lbrace; &NewLine; parent&colon;&colon;&lowbar;&lowbar;construct&lpar;&rpar;&semi; &NewLine; &NewLine; &sol;&sol; โหลด simple&lowbar;html&lowbar;dom &NewLine; include APPPATH &period; 'third&lowbar;party&sol;simple&lowbar;html&lowbar;dom&period;php'&semi; &NewLine; &rcub; &NewLine; &NewLine; function index&lpar;&rpar; &lbrace; &NewLine; &NewLine; &dollar;html &equals; new Simple&lowbar;html&lowbar;dom&lpar;&rpar;&semi; &sol;&sol; ประกาศใช้งานคลาส &NewLine; &dollar;html-&gt&semi;load&lowbar;file&lpar;'http&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;'&rpar;&semi; &sol;&sol;เลือกโหลดเว็บไซต์ &NewLine; foreach &lpar;&dollar;html-&gt&semi;find&lpar;'img'&rpar; as &dollar;element&rpar; &lbrace; &sol;&sol; ใช้เมธอด find ค้นหา img element &NewLine; echo &dollar;element-&gt&semi;src &period; '&lt&semi;br&gt&semi;'&semi; &sol;&sol; แสดงค่าในแอททริบิวร์ src &NewLine; &rcub; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;&quest;&gt&semi;<&sol;pre>&NewLine;<p>4&period; และเมื่อเราทดสอบการทำงานโดยเปิดไปที่ HtmlController จะได้ Output เป็น path ของรูปทั้งหมดที่มีในหน้าเว็บ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default highlight&colon;0 decode&colon;true " >onesignal-เว็บ-push-notification-6-730x410&period;jpg &NewLine;จากนั้นจะขึเนสถานะการลงแอปให้รอจนกว่าจะดำเนินการจนเสร็จ-730x410&period;png &NewLine;php-codeigniter-framework-730x304&period;png &NewLine;วิธีทำลิงค์เพิ่มเพื่อนโปรแกรม-line-730x410&period;png &NewLine;google-adword-ไฟล์-excel-730x410&period;jpg &NewLine;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period; &NewLine;<&sol;pre>&NewLine;<p>การใช้งานคลาสในลักษณะนี้นิยมนำไปใช้ในการดึงข้อมูลบางอย่างจากเว็บไซต์ที่ให้บริการข้อมูลสาธารณะที่ยังไม่มี API ให้ใช้งาน ตัวอย่างเช่น Google Currency บริการแปลงค่าเงินจากสกุลเงินทั่วโลก เราสามารถใช้คลาสนี้ในการไปดึงข้อมูลค่าเงินมาแสดงภายในเว็บไซต์ของเราได้ หรือแม้แต่การดึงข้อมูลฉลากกินแบ่งรัฐบาล ก็สามารถทำได้เช่นกัน<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;

Exit mobile version