0

我正在使用启用了 cURL 的 XAMPP,我尝试解析 php 生成的 xml,但出现此错误,请问有什么帮助吗?我的 search.php 代码:

   <?php
header("Content-type: text/xml");
$conn=mysql_connect("localhost","root","");
mysql_select_db("places");
$location = $_GET['location'];
$type = $_GET['type'];
if($location!="" && $type != "" ){
    $result=mysql_query("select * from accommodation WHERE location='$location' AND type= '$type'");
    }

elseif($location != "")
    {$result=mysql_query("select * from accommodation WHERE location='$location'");
}

else{echo "ERROR";
}

echo"<places>";
while($row=mysql_fetch_array($result))
    {
        echo "<place>";
        echo "<placeid>$row[ID]</placeid>";
        echo "<name>$row[name]</name>";
        echo "<type>$row[type]</type>";
        echo "<location>$row[location]</location>";
        echo "<availableroom>$row[availability]</availableroom>";
        echo "</place>";
    }


echo"</places>";

mysql_close($conn);
?>

这是我的 cURL 代码:我使用 POST 从另一个页面中的表单获取位置和类型。我也使用了---> echo "The server sent back :"。htmlentities($response); 但我没有得到任何回应,只有相同的错误消息

 <?php
    $l = $_POST['location'];
    $t = $_POST['type'];
    $connection = curl_init();
    curl_setopt($connection, CURLOPT_URL,"search.php?location=$l&type=$t");
    curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($connection,CURLOPT_HEADER, 0);
    $response = curl_exec($connection);
    echo "The server sent back : ". htmlentities($response);
    $xml = simplexml_load_string($response);
    for($index=0; $index < count($xml->hotel); $index++)
    {
        echo $xml->hotel[$index]->hotelid . "<br />";
        echo $xml->hotel[$index]->name . "<br />";
        echo $xml->hotel[$index]->type . "<br />";
        echo $xml->hotel[$index]->location . "<br />";
        echo $xml->hotel[$index]->availableroom . "<br />";
    }


    ?>
4

1 回答 1

0

我不清楚您是使用 curl 从外部主机还是本地主机获取 XML(在本地执行此操作没有多大意义),但无论哪种方式"search.php?location=$l&type=$t"都不是 curl 可以获取的有效 URL,这就是为什么$result为空,这就是为什么$xml是假而不是对象,这就是为什么你得到你得到的错误。

将协议和主机名添加到您的 URL。确保$xml != false之后$xml = simplexml_load_string($response)

于 2012-04-19T01:30:18.590 回答