我无法重复爆炸的lat和lng值。保持只返回数据库中的一对(x,y)值。
请参阅以下代码:
<?php
$feed = file_get_contents("http://api.geograph.org.uk/syndicator.php?key=hfALmNnpQ&i=14102530&page=1&format=media&expand=1&perpage=100");
$xml = new SimpleXmlElement($feed);
foreach ($xml->item as $entry){
echo $entry->title;
echo $entry->description;
echo $entry->link;
// use that namespace
$dc = $entry->children('http://purl.org/dc/elements/1.1/');
$georss = $entry->children('http://www.georss.org/georss');
echo $entry->children('http://purl.org/dc/elements/1.1/')->creator;
echo $entry->children('http://www.georss.org/georss')->point
list($lat,$lng) = explode(' ', $entry->children('http://www.georss.org/georss')->point);
}
?>
下面的代码显示了数据库操作:
<?php
require 'table.php';
// Opens a connection to a PostgresSQL server
$connection = pg_connect("dbname=postgis user=postgres password=****");
// Execute query
foreach ($xml->item as $entry) {
$query = "INSERT INTO geognew(title, link, author, latitude, longitude) VALUES ('" . $entry->title . "', '" . $entry->link . "', '" . $entry->children('http://purl.org/dc/elements/1.1/')->creator . "', '" . $lat . "', '" . $lng . "')";
$result = pg_query($query);
printf ("These values are inserted into the database - %s %s %s", $entry->title, $entry->link, $entry->children('http://purl.org/dc/elements/1.1/')->creator, $lat, $lng);
}
pg_close();
?>
由于 Yemi
答案 0 :(得分:0)
我没有在课堂上定义SimpleXmlElement::item()
。是的意思是SimpleXmlElement::children?
意思是:
foreach ($xml->children() as $entry) { ... }
答案 1 :(得分:0)
最好保存数组中的lat / lng。因为反复迭代xml是一种过度杀伤。
// define this $points outside the foreach loop
$points = array();
foreach(...){
....
list($lat,$lng) = explode(' '
, (string)($entry->children('http://www.georss.org/georss')->point));
$points[] = array('lat'=> $lat, 'long' => $lng);
}
这是一个完整的代码,只能通过纬度和经度进行迭代。
$xml = simplexml_load_string($feed);
$namespaces = $xml->getNameSpaces(true);
foreach ($xml->item as $entry){
$georss = $entry->children($namespaces['georss']);
list($lat,$lng) = explode(' ', (string)$georss->point);
echo "$lat, $lng\n";
}