Php 使用xpath或其他方法获取图像
我需要从远程页面获取图像,我尝试了xpath,但我被告知它不起作用,因为img没有nodevalue 然后有人建议我使用getAttribute,但我不知道如何让它工作 有什么建议吗? 这是我的密码Php 使用xpath或其他方法获取图像,php,html,xml,xpath,getattribute,Php,Html,Xml,Xpath,Getattribute,我需要从远程页面获取图像,我尝试了xpath,但我被告知它不起作用,因为img没有nodevalue 然后有人建议我使用getAttribute,但我不知道如何让它工作 有什么建议吗? 这是我的密码 <?php libxml_use_internal_errors(true); //Setting content type to xml! header('Content-type: application/xml'); //POST Field name is bWV0aG9k $
<?php
libxml_use_internal_errors(true);
//Setting content type to xml!
header('Content-type: application/xml');
//POST Field name is bWV0aG9k
$url_prefix = $_GET['bWV0aG9k'];
$url_http_request_encode = strpos($url_prefix, "http://");
//Checking to see if url has a http prefix
if($url_http_request_encode === false){
//does not have, add it!
$fetchable_url_link_consistancy_remote_data = "http://".$url_prefix;
}
else
//has it, do nothing
{
$fetchable_url_link_consistancy_remote_data = $url_prefix;
}
//Creating a new DOM Document on top of pre-existing one
$page = new DOMDocument();
//Loading the requested file
$page->loadHTMLFile($fetchable_url_link_consistancy_remote_data);
//Initliazing xpath
$xpath = new DOMXPath($page);
//Search parameters
//Searching for title attribute
$query = "//title";
//Searching for paragraph attribute
$query1 = "//p";
//Searching for thumbnails
$query2 = "//img";
//Binding the attributes to xpath for later use
$title = $xpath->query($query);
$paragraph = $xpath->query($query1);
$images = $xpath->query($query2);
echo "<remotedata>";
//Echoing the attributes
echo "<title-render>".$title->item(0)->nodeValue."</title-render>";
echo "<paragraph>".$paragraph->item(0)->nodeValue."</paragraph>";
echo "<image_link>".$images->item(0)->nodeValue."</image_link>";
echo "</remotedata>";
?>
loadHTMLFile($fetchable\u url\u link\u constancy\u remote\u data);
//初始化xpath
$xpath=newdomxpath($page);
//搜索参数
//搜索标题属性
$query=“//title”;
//搜索段落属性
$query1=“//p”;
//搜索缩略图
$query2=“//img”;
//将属性绑定到xpath以供以后使用
$title=$xpath->query($query);
$paragration=$xpath->query($query1);
$images=$xpath->query($query2);
回声“;
//呼应属性
回显“$title->item(0)->nodeValue.”;
回显“$段落->项目(0)->节点值”;
回显“$images->item(0)->nodeValue.”;
回声“;
?>
您应该获得图像标记的源属性
$images->item(0)->getAttribute('src');
如果这是正常的xhtml,img没有值,则需要img/@src的值。提示:不要使用注释来说明下一行的作用<代码>//将内容类型设置为xml当下一行显示
header('Content-type:application/xml')时,code>是完全多余的代码>。所有其他评论都是一样的。除此之外:如果看不到您正在处理的XML,没有人能够帮助您。请尝试签入phpquery。。。这应该会有帮助。您是否建议我先使用getElementByTagName来确保只获取img标记的src?我假设您已经在dom中加载了html$query2=“///img”//将选择所有图像标记$images=$xpath->query($query2);//$IMAG已选择所有图像标记$images->item(0)->getAttribute('src')//将为您提供第一个图像的src属性值