2013-12-13 34 views
1

說我有這個環節外部URL得到圖片src使用PHP

$url ="http://www.egyptian-planet.com/news-90.html"; 

//here get img or <meta property="og:image" from link 

我怎樣才能得到IMG SRC或(使用PHP)得到鏈接<meta property="og:image"

任何幫助

+0

使用'file_get_contents'或'cURL'到dow下載頁面,以及一個HTML DOM解析器來解析它。 – Barmar

回答

2

您可以使用捲曲這樣的: -

function getHTML($url,$timeout) 
{ 
     $ch = curl_init($url); // initialize curl with given url 
     curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute 
     curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error 
     return @curl_exec($ch); 
} 

$html=getHTML("http://www.egyptian-planet.com/news-90.html",10); 

preg_match_all('/<img .*src=["|\']([^"|\']+)/i', $html, $matches); 
foreach ($matches[1] as $key=>$value) { 
    echo $value."<br>"; 
} 

另一種解決方案: -

您還可以使用SimpleHtml庫提取圖像像這樣: -

include_once("simple_html_dom.php"); 

$html=getHTML("http://www.egyptian-planet.com/news-90.html"); 

// Find all images on webpage 
foreach($html->find("img") as $element) 
echo $element->src . '<br>';