Php DomCrawler filterXpath不总是提供完整的URL
对于我的项目,我使用解析页面和提取图像 代码: 相关部分在底部。这将在某些时候起作用。然而,偶尔我会出错。例如,如果Php DomCrawler filterXpath不总是提供完整的URL,php,goutte,domcrawler,Php,Goutte,Domcrawler,对于我的项目,我使用解析页面和提取图像 代码: 相关部分在底部。这将在某些时候起作用。然而,偶尔我会出错。例如,如果$url是,它将抛出以下错误: 错误异常(E_警告) getimagesize(/images/branding/googlelogo/1x/googlelogo_白色_背景色_272x92dp.png): 无法打开流:没有这样的文件或目录 如果Idd($image)在这种情况下,$image等于“/images/branding/googlelogo/1x/googlelogo\
$url
是,它将抛出以下错误:
错误异常(E_警告)
getimagesize(/images/branding/googlelogo/1x/googlelogo_白色_背景色_272x92dp.png):
无法打开流:没有这样的文件或目录
如果Idd($image)
在这种情况下,$image
等于“/images/branding/googlelogo/1x/googlelogo\u white\u background\u color\u 272x92dp.png”
然而,如果我尝试使用一个没有给我错误的网站,比如,dd($image)代码>返回“https://www.harvard.edu/sites/default/files/feature_item_media/Kremer900x600.jpg“
换句话说,我没有得到完整的URL。如何更正此问题?预先设置与方案和主机的相关链接。您可以在$url
上使用parse_url
来提取方案和主机,也可以在$image
上使用相同的功能来检测是否设置了方案/主机。预先设置与方案和主机的相关链接。您可以在$url
上使用parse_url
来提取方案和主机,也可以在$image
上使用相同的功能来检测是否设置了方案/主机
$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
'timeout' => 15,
));
$goutteClient->setClient($guzzleClient);
try {
$crawler = $goutteClient->request('GET', $url);
$crawlerError = false;
} catch (RequestException $e) {
$crawlerError = true;
}
if ($crawlerError == false) {
//find open graph image
try {
$file = $crawler->filterXPath("//meta[@property='og:image']")->attr('content');
} catch (\InvalidArgumentException $e) {
$file = null;
}
//if that fails, find the biggest image in the DOM
if (!$file) {
$images = $crawler
->filterXpath('//img')
->extract(array('src'));
$files = [];
foreach ($images as $image) {
$attributes = getimagesize($image);
//stopping here since this is where i'm getting my error