cURL似乎不适用于while循环

时间:2010-07-21 18:42:32

标签: php xml curl

我会发布整件事,因为否则会有点混乱:

<?php
echo "<html>
 <head>
  <title>ARMORY.</title>
  <meta http-equiv='Content-Type' content='text/html' charset=iso-8859-1>
 </head>
 <body>
 <table width='50%' border='1' cellpadding='10' cellspacing='10'>";


$server = "Sunstrider";
$guild = "Mist";
$url='http://eu.wowarmory.com/guild-info.xml?r='.$server.'&gn='.$guild.'&p=1';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_USERAGENT,  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
$xml = curl_exec($ch);
$rosterxml = new SimpleXMLElement($xml);
curl_close($ch);
$array = array();



foreach($rosterxml->guildInfo->guild->members->character as $char)
  if(strtolower($char['level']) === '80')
  {
        $array[] = $char['name']."<br />";

  }

  echo "
   <tr>
    <td valign='middle'>Name</td>
    <td valign='middle'>TEST</td>
   </tr>";  
$i = 0;
while($array[$i] != null) 
{

 $name = $array[$i];
 $raidurl='http://eu.wowarmory.com/character-achievements.xml?r='.$server.'&cn='.$name.'&c=168';
 $ch = curl_init();
 curl_setopt ($ch, CURLOPT_URL, $raidurl);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt ($ch, CURLOPT_USERAGENT,  "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
 $xml2 = curl_exec($ch);
 $achievementxml = new SimpleXMLElement($xml2);
 curl_close($ch);
 var_dump($achievement);

 echo "<tr>
  <td>$array[$i]</td>
  <td></td>
  </tr>";
 $i++;



}

?>

 </body>
</html>

$ achievement的var_dump只是一遍又一遍地产生NULL(显然是由于循环)而不是有关该数组的任何信息。执行$ rosterxml的var_dump会产生预期的效果,因此cURL似乎在while循环之外正常工作。

1 个答案:

答案 0 :(得分:5)

这是因为您的变量名为$achievementxml而不是$achievement

我建议您使用error_reporting=E_ALL进行编码,这样就可以捕获这样的错误。未定义的变量将导致E_NOTICE级错误消息。