ฉันต้องการดึงโค้ด HTML ของลิงค์ (หน้าเว็บ) ใน PHP ตัวอย่างเช่นหากลิงก์คือ
จากนั้นฉันต้องการโค้ด HTML ของหน้าที่ให้บริการ ฉันต้องการดึงโค้ด HTML นี้และเก็บไว้ในตัวแปร PHP
ฉันจะทำเช่นนี้ได้อย่างไร?
ฉันต้องการดึงโค้ด HTML ของลิงค์ (หน้าเว็บ) ใน PHP ตัวอย่างเช่นหากลิงก์คือ
จากนั้นฉันต้องการโค้ด HTML ของหน้าที่ให้บริการ ฉันต้องการดึงโค้ด HTML นี้และเก็บไว้ในตัวแปร PHP
ฉันจะทำเช่นนี้ได้อย่างไร?
คำตอบ:
หากเซิร์ฟเวอร์ PHP ของคุณอนุญาต url fopen wrapper วิธีที่ง่ายที่สุดคือ:
$html = file_get_contents('/programming/ask');
หากคุณต้องการการควบคุมมากขึ้นคุณควรดูฟังก์ชันcURL :
$c = curl_init('/programming/ask');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
นอกจากนี้หากคุณต้องการจัดการกับหน้าที่ดึงข้อมูลคุณอาจต้องการลองใช้ตัวแยกวิเคราะห์ php DOM ฉันพบว่าPHP Simple HTML DOM Parser ใช้งานง่ายมาก
คุณอาจต้องการตรวจสอบไลบรารี YQL จาก Yahoo: http://developer.yahoo.com/yql
งานในมือนั้นง่ายพอ ๆ
select * from html where url = 'http://stackoverflow.com/questions/ask'
คุณสามารถทดลองใช้ในคอนโซลได้ที่: http://developer.yahoo.com/yql/console (ต้องเข้าสู่ระบบ)
ดู screencast ของ Chris Heilmanns สำหรับแนวคิดดีๆที่คุณสามารถทำอะไรได้อีก: http://developer.yahoo.net/blogs/theater/archives/2009/04/screencast_collating_distributed_information.html
วิธีง่ายๆ:ใช้file_get_contents():
$page = file_get_contents('http://stackoverflow.com/questions/ask');
โปรดทราบว่าคุณallow_url_fopenต้องอยู่trueในตัวคุณphp.iniจึงจะสามารถใช้ห่อหุ้ม fopen ที่ทราบ URL
วิธีขั้นสูงเพิ่มเติม:หากคุณไม่สามารถเปลี่ยนการกำหนดค่า PHP ของคุณallow_url_fopenเป็นfalseค่าเริ่มต้นและหากติดตั้ง ext / curl ให้ใช้cURLไลบรารีเพื่อเชื่อมต่อไปยังหน้าที่ต้องการ
คุณสามารถใช้ file_get_contents ได้หากคุณต้องการจัดเก็บซอร์สเป็นตัวแปร
$url = file_get_contents('http://example.com');
echo $url;
โซลูชันนี้จะแสดงหน้าเว็บบนไซต์ของคุณ อย่างไรก็ตาม curl เป็นตัวเลือกที่ดีกว่า
ดูฟังก์ชั่นนี้:
include_once('simple_html_dom.php');
$url="http://stackoverflow.com/questions/ask";
$html = file_get_html($url);
คุณสามารถรับโค้ด HTML ทั้งหมดเป็นอาร์เรย์ (แบบแยกวิเคราะห์) โดยใช้โค้ดนี้ดาวน์โหลดไฟล์ 'simple_html_dom.php' ที่นี่ http://sourceforge.net/projects/simplehtmldom/files/simple_html_dom.php/download
วิธีง่ายๆในการรับเนื้อหาจาก URLมีสองวิธีดังนี้
1) วิธีแรก
เปิดใช้ Allow_url_include จากโฮสติ้งของคุณ (php.ini หรือที่ไหนสักแห่ง)
<?php
$variableee = readfile("http://example.com/");
echo $variableee;
?>
หรือ
2) วิธีที่สอง
เปิดใช้งาน php_curl, php_imap และ php_openssl
<?php
// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php
function get_dataa($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$variableee = get_dataa('http://example.com');
echo $variableee;
?>
คุณสามารถใช้เมธอด DomDocument เพื่อรับตัวแปรระดับแท็ก HTML แต่ละรายการได้เช่นกัน
$homepage = file_get_contents('https://www.example.com/');
$doc = new DOMDocument;
$doc->loadHTML($homepage);
$titles = $doc->getElementsByTagName('h3');
echo $titles->item(0)->nodeValue;
$output = file("http://www.example.com");ใช้งานไม่ได้จนกว่าฉันจะเปิดใช้งาน: allow_url_fopen, allow_url_include,และfile_uploadsในphp.iniPHP7
ฉันลองใช้รหัสนี้แล้วและมันใช้ได้ผลสำหรับฉัน
$html = file_get_contents('www.google.com');
$myVar = htmlspecialchars($html, ENT_QUOTES);
echo($myVar);