使用php获取图像src的外部url

使用php获取图像src的外部url,php,Php,假设我有这个链接 $url ="http://www.egyptian-planet.com/news-90.html"; //here get img or <meta property="og:image" from link $url=”http://www.egyptian-planet.com/news-90.html"; //这里获取img或您可以像这样使用cURL:- function getHTML($url,$timeout) { $ch = curl_

假设我有这个链接

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

//here get img or <meta property="og:image" from link
$url=”http://www.egyptian-planet.com/news-90.html";

//这里获取img或您可以像这样使用cURL:-

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>";
}
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>';
函数getHTML($url,$timeout) { $ch=curl\u init($url);//使用给定的url初始化curl curl\u setopt($ch,CURLOPT\u USERAGENT,$\u SERVER[“HTTP\u USER\u AGENT”]);//set USERAGENT curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//将响应写入变量 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);//如果有重定向,请遵循重定向 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);//执行的最大秒数 curl_setopt($ch,CURLOPT_FAILONERROR,1);//遇到错误时停止 返回@curl_exec($ch); } $html=getHTML(“http://www.egyptian-planet.com/news-90.html",10); preg_match_all('/$value){ 回声$value.“
”; }
另一种解决方案:-

您还可以使用库提取图像,如下所示:-

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>";
}
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>';
include_once(“simple_html_dom.php”);
$html=getHTML(“http://www.egyptian-planet.com/news-90.html");
//查找网页上的所有图像
foreach($html->find(“img”)作为$element)
echo$element->src'
';
使用
file\u get\u contents
cURL
下载页面,并使用HTML DOM解析器对其进行解析。