我有以下网址:
http://localhost:8000/PHPTrialProject/showPlace.php?lat=28.6139391&lng=77.20902120000005
我已经从中提取了查询字符串并存储在两个 PHP 变量中:
$latitude=$_GET['lat'];
$longitude=$_GET['lng'];
echo("latitude is : ".$latitude);
echo("longitude is : ".$longitude);
然后我将它们附加到我的下一个 URL 并尝试将其加载到 DOMDocument 中。我尝试了两种方法:
$url="http://api.openweathermap.org/data/2.5/weather?lat=".$latitude."&lon=".$longitude;
$xml3 = $url;
$xmlDoc3 = new DOMDocument();
$xmlDoc3->load($xml3);
和
$xml3 = ("http://api.openweathermap.org/data/2.5/weather?lat=".$latitude."&lon=".$longitude);
$xmlDoc3 = new DOMDocument();
$xmlDoc3->load($xml3);
但是,他们都给了我以下错误:
警告:DOMDocument::load():需要开始标记,在 http://api.openweathermap.org/data/2.5/weather?lat=28.6139391&lon=77.20902120000005 中找不到“<” , line: 1 in C:\xampp\htdocs\PHPTrialProject\showPlace.php 第 98 行
我该如何解决?
最佳答案
问题是您只是向它发送 URL 而不是页面本身,请使用 file_get_contents或 cURL然后将其发送到您的 DOM 文档。
例如
$xml = file_get_contents("http://api.openweathermap.org/data/2.5/weather?lat=".$latitude."&lon=".$longitude);
$xmlDoc3 = new DOMDocument();
$xmlDoc3->load($xml);
关于php - 无法在 DOMDocument 中加载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29010892/