我正在尝试为我创建一些辅助工具。因为我创建了一个交易聚合器,我需要将 xpath 放入我的数据库中,但首先我需要创建一些工具来帮助我进行验证。
我有一个 html 文件,我需要在其中放置 xpath,然后通过操作转到 generatexpath.php 文件,我需要在其中查看我使用 xpath 编写的内容。所以...
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="post" action="generatingxpath.php">
<p>
<label for="websource"></label>
Web source:
<input name="websource" type="text" id="websource" size="70">
is:</p>
<p>
<label for="links"></label>
Links:
<input name="links" type="text" id="links" size="77">
OK!
</p>
<p>
<label for="title"></label>
Title:
<input name="title" type="text" id="title" size="78">
is:</p>
<p>
<label for="image"></label>
Image:
<input name="image" type="text" id="image" size="76">
show:</p>
<p>
<label for="disscount"></label>
Disscount:
<input name="disscount" type="text" id="disscount" size="72">
is:</p>
<p>
<label for="price"></label>
Price:
<input name="price" type="text" id="price" size="77">
is:</p>
<p>
<label for="description"></label>
Desription:
<input name="desription" type="text" id="description" size="72">
is:</p>
<p>
<label for="coordinates"></label>
Coordinates:
<input name="coordinates" type="text" id="coordinates" size="70">
lat,lng is:</p>
<p>
<input type="submit" name="Submit" id="Submit" value="Preview">
or
<input type="submit" name="Add to database" id="Add to database" value="Add To Database">
</p>
<p> </p>
</form>
</body>
</html>
我的 php 文件是:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>GENERATING XPATH</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
libxml_use_internal_errors(true);
$websource = $_POST["websource"];
$links = $_POST["links"];
$title = $_POST["title"];
$image = $_POST["image"];
$disscount = $_POST["disscount"];
$price = $_POST["price"];
$description = $_POST["description"];
$coordinates = $_POST["coordinates"];
//start generating xpath script
$dom = new DOMDocument();
@$dom->loadHTMLFile($websource);
$xpath = new DOMXPath($dom);
$entries = $xpath->query($links);
$output = array();
$i = 1;
foreach($entries as $e) {
$dom2 = new DOMDocument();
@$dom2->loadHTMLFile($websource . $e->textContent);
$xpath2 = new DOMXPath($dom2);
$data = array();
$data['websource']= ($websource . $e->textContent);
$data['title'] = trim($xpath2->query($title)->item(0)->textContent);
$data['description'] = trim($xpath2->query($description)->item(0)->textContent);
$data['image'] = trim($xpath2->query($image)->item(0)->textContent);
$string = $xpath2->query($coordintes)->item(1)->textContent;
preg_match_all('#(([0-9-]+){1,3}.([0-9]+))#is', $string, $matches);
$data['lat']=$matches[1];
$data['lng']=$matches[2];
$data['disscount'] = trim($xpath2->query($disscount)->item(0)->textContent);
$data['price'] = trim($xpath2->query($price)->item(0)->textContent);
//print to see that all is fine or need to change some xpath direction
echo $data['websource'];
echo $data['links'];
echo $data['title'];
echo $data['image'];
echo $data['disscount'];
echo $data['price'];
echo $data['lat'];
echo $data['lng'];
echo $data['description'];
$output[] = $data;
}
?>
</body>
</html>
我首先在 67 行中遇到了几个错误。对于每个...等
是否有一些简单的解决方案可以让我在同一页面上获得结果,所以要使用 xpath 从某个网站获取文本,但要在 html 页面上显示... ajax?或者其他的东西?
我的问题的最佳解决方案是什么?如何解决这个问题?