0

I am starting out with PHP and in my project I'm trying to fetch XML data from a url and parsing it for data. Now the url that I use is being fetched from a webpage and hence I know that it is correct and it exists. Still, when I try to execute the page I get the following errors: Warning: DOMDocument::load() [domdocument.load]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\Siddharth Dahiya\Dropbox\www\Site\includes\tw_worlds.php on line 138 The code in question is:

//Problem function
function PopulateSettings()
	{
		set_time_limit(60000); //Lots of data hence large load time...
		$rtnVal = "Inside PopulateSettings()...<br />";
		$serverList = GetAllServers();
		$runs = 0;
		//$rtnVal .= "<hr />" . print_r($serverList); //Give the right list

		foreach($serverList as $serverURL => $serverName)
		{
			$worldList = GetAllWorlds($serverURL);

                        //Each server has a different world...
			foreach($worldList as $worldID => $worldTitle)
			{
				$runs++;
				$url = "http://" . $worldID . "." . $serverName . "/interface.php?func=get_config";
				//$rtnVal .= "<hr />Server: {$url}";				
				if(file_exists($url))
				{
					$rtnVal .= "{$url} : Exists...";
					//break;
				} else{
					$rtnVal .= "{$url} : Does Not Exists...";
					//break;
				}
				
				$doc = new DomDocument;
/*PROBLEM CODE-->*/		$doc->load($url); //Problem!!!
				//$rtnVal .= "<br /> Nodes:\n";
				$settings["worldID"] = $worldID;
				$settings["serverID"] = $serverName;
				
				$speedNode = $doc->getElementsByTagName("speed");
				$settings["speed"] = $speedNode->item(0)->nodeValue;
				
				$unitSpeedNode = $doc->getElementsByTagName("unit_speed");
				$settings["unitSpeed"] = $unitSpeedNode->item(0)->nodeValue;
				
				$pallyNode = $doc->getElementsByTagName("knight");
				$settings["paladin"] = $pallyNode->item(0)->nodeValue;
				
				$archerNode = $doc->getElementsByTagName("archer");
				$settings["archer"] = $archerNode->item(0)->nodeValue;
				
				$churchNode = $doc->getElementsByTagName("church");
				$settings["church"] = $churchNode->item(0)->nodeValue;
				
				$bonusNode = $doc->getElementsByTagName("bonus_new");
				$settings["bonus"] = $bonusNode->item(0)->nodeValue;
				
				//$rtnVal .= "<hr />" . print_r($settings);
				/*if($runs > 30)
					break;*/
				AddSettings($settings);
			}
			break;
		}
		return $rtnVal;
	}

//Used to get the servers
function GetAllServers()
	{
		$rtnVal = "Inside GetAllServers()..<br />";
		$fetchData = fetch_page("http://www.tribalwars.net");
		//$rtnVal .= "<hr /> {$fetchData} <hr />";
		$start = strpos($fetchData, "id=\"flags\"") + strlen("id=\"flags\">");
		$end = strpos($fetchData, "</ul>", $start);
		$optionsArray = preg_split("/\n/", substr($fetchData, $start, $end - $start));
		$valueArray;
		foreach($optionsArray as $i =>$value)
		{
			$sQ = strpos($value, "href=\"");
			$eQ = strpos($value, "?");
			$wKey = substr($value, $sQ+6, $eQ-$sQ-6);
			$sRB = strpos($value, ">", $eQ) + 1;
			$eRB = strrpos($value, "<", $sRB) - 4;
			if(($eRB-$sRB) > 0)
				$wValue = substr($value, $sRB, $eRB-$sRB);
			else
				$wValue = substr($value, $sRB, strlen($value)-$sRB);
			$valueArray[$wKey] = $wValue;
		}
		
		//DEBUG - Gives correct output...
                //$rtnVal .= "The list...<br />\n<pre>";
		//$rtnVal .= print_r($optionsArray) . "</pre><br />";
		//$rtnVal .= "<hr /><pre>" . print_r($optionsArray) . "</pre><hr />";
		//$rtnVal .= "<hr /><pre>" . print_r($valueArray) . "</pre><hr />";
		//$rtnVal .= $fetchData;
		unset($valueArray['0']);
		return $valueArray;
		//return $rtnVal;
	}

/*
	 * Retrive data from the specified server about all the worlds. 
	 */
	function GetAllWorlds($server)
	{
		//$rtnVal = "Inside GetAllWorlds()...<br />";
		$fetchData = fetch_page($server);
		$start = strpos($fetchData, "<option");
		$end = strrpos($fetchData, "</option>");
		$optionsArray = preg_split("/\n/", substr($fetchData, $start, $end - $start));
		$valueArray;
		foreach($optionsArray as $i =>$value)
		{
			$sQ = strpos($value, "\"");
			$eQ = strrpos($value, "\"");
			$wKey = substr($value, $sQ+1, $eQ-$sQ-1);
			$sRB = strpos($value, ">");
			$eRB = strrpos($value, "<");
			if(($eRB-$sRB) > 0)
				$wValue = substr($value, $sRB, $eRB-$sRB);
			else
				$wValue = substr($value, $sRB, strlen($value)-$sRB);
			$valueArray[$wKey] = $wValue;
		}
		

                //DEBUG -- Give correct output...
		//$rtnVal .= "<hr /><pre>" . print_r($optionsArray) . "</pre><hr />";
		//$rtnVal .= "<hr /><pre>" . print_r($valueArray) . "</pre><hr />";
		//$rtnVal .= $fetchData;
		return $valueArray;
	}

Can anyone see what might be broken with the DOM object? or can anyone suggest what method I can use to check that the XML is being sent by the server?

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by sid78669
0

I solved this myself. Was able to do some serious google and found the following code at: http://php.assistprogramming.com/check-website-status-using-php-and-curl-library.html

function Visit($url)
{

	$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();

	curl_setopt ($ch, CURLOPT_URL,$url );

	curl_setopt($ch, CURLOPT_USERAGENT, $agent);

	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

	curl_setopt ($ch,CURLOPT_VERBOSE,false);

	curl_setopt($ch, CURLOPT_TIMEOUT, 5);

	$page=curl_exec($ch);

	//echo curl_error($ch);

	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	curl_close($ch);

	if($httpcode>=200 && $httpcode<300) return true;

	else return false;

}

if(Visit("http://www.domain.com"))

           echo "Website OK"."n";

      /*if site down*/

     else

          echo "Website DOWN";

Hope this helps any one else looking for this code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.