我正在创建一个交易聚合器,但首先我需要将 xpath 放入数据库,但在此之前我需要在我的字段中输入 xpath 和网站以查看我会得到什么结果......所以某种类型的 xpath 验证工具:
这是我在文本字段中输入 xpath 地址的代码:
xpathadd.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
</head>
<body>
<script>
addToDatabase = function() {
alert("addToDatabase called");
$.post("generatingxpath.php", {
websource: $('#websource').val(),
links: $('#links').val(),
title: $('#title').val(),
image: $('#image').val(),
discount: $('#discount').val(),
price: $('#price').val(),
description: $('#description').val(),
coordinates: $('#coordinates').val()
}, function(data) {
$("#result").html(data);
});
};
</script>
<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="discount"></label>
Disscount:
<input name="discount" type="text" id="discount" 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="description" 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">
<a href="javascript:addToDatabase();">Add to database</a>
</p>
<p> </p>
</form>
<div id="result"></div>
</body>
</html>
并生成xpath.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 行 /home/pluspon/public_html/test/generatingxpath.php 中的 foreach() 提供的参数无效???为什么?问题在哪里?