file_get_contents() in php get content ของเว็บไซต์หรือไฟล์

22 May 2018,
Share: 

file_get_contents() เป็น function ภาษา PHP ใช้สําหรับดึงข้อมูลจากไฟล์ หรือ url มาดูตัวอย่างการใช้งานกันเลยครับ

Example1: ดึงข้อมูลจากไฟล์ demo.txt ขึ้นมาแสดง ไฟล์ demo.txt มีเนื้อหาดังนี้

demo file php.

ไฟล์ index.php มีเนื้อหาดังนี้

<?php
    $homepage = file_get_contents('demo.txt');
    echo $homepage;
?>

Output: แสดงข้อความ “demo file php.” บนหน้าจอ Example2: ดึงข้อมูลจากเว็บ google.com มาแสดงผล

<?php
    $homepage = file_get_contents('http://www.google.com/');
    echo $homepage;
?>

Output: ระบบจะ echo ข้อมูล String html ของเว็บ google.com ออกมาจะได้เป็นหน้าจอนี้

Image


Copyright © 2019 - 2025 thiti.dev |  v1.56.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Nostr   Youtube channel   Telegram   Email contact   วงแหวนเว็บ