Help with foreach()

I have this script im putting together and it works when it wants to lol seriously! can anyone see what i did wrong

i get this error most of the time:

Warning: Invalid argument supplied for foreach() in [path]/newsfeed.php on line 17
<?php		

		$guild = "Enclave"; // Modify this to your Guild Name
		$realm = "Shattered-Hand";     // Modify this to your Realm Name
		$max = "20";			// How many records you want returned



		//http://us.battle.net/api/wow/guild/Elune/Damnation?fields=news
		$api_guild_news_url = 'http://us.battle.net/api/wow/guild/' . $realm . '/' . $guild . '?fields=news';
		
		$file_contents = @file_get_contents($api_guild_news_url); // omit warnings

		$guildNews = json_decode($file_contents, true);

		echo '<center><font color="#FFD100">Guild Level: ' . $guildNews['level'] . '</font></center><br>';
		foreach($guildNews['news'] as $news)
		{
			if($counter++ == $max)
			{
				break;
			}
			switch($news['type'])
			{
				case "itemPurchase" :
				{
					echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' purchased </font>' . ' <a href="http://www.wowhead.com/item=' . $news['itemId'] . '" target="_blank">' . '<font color="'. getColorByItemId($news['itemId']) . '">' . getPropertyByItemId($news['itemId'])  . '</a></font><br>';			
					echo '<div class="widget_post_bit"></div>' ;
;
					break;
				}
				case "itemLoot" :
				{
					echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' obtained </font>' . ' <a href="http://www.wowhead.com/item=' . $news['itemId'] . '" target="_blank">' . ' <font color="'. getColorByItemId($news['itemId']) . '">' . getPropertyByItemId($news['itemId']) . '</a></font><br>';
					echo '<div class="widget_post_bit"></div>' ;
					break;
				}
				case "playerAchievement" :
				{
					echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' earned the achievement </font>' . '<a href="http://www.wowhead.com/achievement=' . $news['achievement']['id'] . '" target="_blank"><font color="#FFD100"> ' . $news['achievement']['title'] . '</a></font><br>';
					echo '<div class="widget_post_bit"></div>' ;
					break;
				}
				case "guildAchievement" :
				{
					echo '<font color="#F9A205">' . $news['character'] . '</font> <font color="#DEB887">' . ' earned the achievement </font>' . '<a href="http://www.wowhead.com/achievement=' . $news['achievement']['id'] . '" target="_blank"><font color="#FFD100">' . $news['achievement']['title'] . '</a></font><br>';
					echo '<div class="widget_post_bit"></div>' ;
					break;
				}
				case "itemCraft" :
				{
					echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' crafted item </font>' . ' <a href="http://www.wowhead.com/item=' . $news['itemId'] . '" target="_blank">' . '<font color="'. getColorByItemId($news['itemId']) . '">' . getPropertyByItemId($news['itemId']) . '</a></font><br>';
					echo '<div class="widget_post_bit"></div>' ;

					break;
				}
			}
		}

		echo '<br><center><a href="http://us.battle.net/wow/en/guild/' . $realm . '/' . $guild . '/news" target="_blank"><font color="#FFD100">View all!</font></a></center>';



function getPropertyByItemId($id)
	{
		$api_item_url = "http://" . "us.battle.net" . "/api/wow/item/" . $id;

		$item = json_decode(file_get_contents($api_item_url), true);

		return $item['name'];
	}

function getPropertyByAchievementsId($id)
	{
		$api_item_url = "http://" . "us.battle.net" . "/api/wow/achievements/" . $id;
		
		$item = json_decode(file_get_contents($api_item_url), true);

		return $item['id'];
	}

function getColorByItemId($id)
	{
		$api_item_url = "http://" . "us.battle.net" . "/api/wow/item/" . $id;

		$item = json_decode(file_get_contents($api_item_url), true);
		
		switch($item['quality'])
		{
			case "4" :
			{
				return "#A335EE";
			}
			case "5" :
			{
				return "#ff8000";
			}
			case "3" :
			{
				return "#0070dd";
			}
		}
	}
function utf8_array_decode($input) 
{ 
	$return = utf8_decode($input); 
	return $return; 
} 
?>

Set $guildnews[‘news’] to a variable before the foreach.

nope still the same error changes i made below. Link to page… ~> http://enclaveraiding.com/newsfeed.php

Thanks for you help btw i really appreciate it.


		$guild = "Enclave"; // Modify this to your Guild Name
		$realm = "Shattered-Hand";     // Modify this to your Realm Name
		$max = "20";			// How many records you want returned
		$guildNewsvar = $guildNews['news'];


		//http://us.battle.net/api/wow/guild/Elune/Damnation?fields=news
		$api_guild_news_url = 'http://us.battle.net/api/wow/guild/' . $realm . '/' . $guild . '?fields=news';
		
		$file_contents = @file_get_contents($api_guild_news_url); // omit warnings

		$guildNews = json_decode($file_contents, true);

		echo '<center><font color="#FFD100">Guild Level: ' . $guildNews['level'] . '</font></center><br>';
		foreach($guildNewsvar as $news)
		{

what version of PHP are you using the file runs fine on my dev server?

To add to what Andrew said,

You have an errant semi-colon in your case “itemPurchase” section.

The error you are getting means that you are not passing a valid php array to your foreach. In other words, $guildNews[‘news’] is not being set to a valid php array when it is created from the json_decode and file_get_contents functions.

This could mean one of three things:

  1. The procedure you are using to get the “news” is incorrect. Since it does work some of the time I would think the problem is more likely one of the other options.
  2. There is no “news” ($guildNews[‘news’]) for the given realm and guild.
  3. There is one “news” item being returned and it is being stored as a non-array object.

If it is appropriate for “news” to be returned sometimes, but not always, you should check that $guildNews[‘news’] is an array with at least one element prior to your foreach …[php]if(is_array($guildNews[‘news’]) && count($guildNews[‘news’]) > 0) foreach($guildNews[‘news’] as $news)[/php]I would suggest doing this regardless of whether an appropriate array is always expected or not. If you don’t and the request fails for any reason, you will throw an error.

Your server could also be timing out when it fetches the data. It takes mine page a bit to load.

You guys were both right, I added your code malasho and it came up… but after a refresh it died… So i increased the scripts TTL and im doing good so far! Thank you very much guys for taking the time to help me.

can some one help me cache the results for about 4 or 5 hours so it doesnt keep requesting it? I think thats my problem cause now it out again.

Worked for me just now.

Maybe change strategies… Call the data every 20 - 30 minutes with a script that is ran by a cron job. This script would store the data as a csv, xml whatever file locally. Then read that local file when you get a page request.

Or just look at your log files and see what error your code is trowing.

Try this:[php]<?php
$guild = “Enclave”; // Modify this to your Guild Name
$realm = “Shattered-Hand”; // Modify this to your Realm Name
$max = “20”; // How many records you want returned

  //http://us.battle.net/api/wow/guild/Elune/Damnation?fields=news
  $api_guild_news_url = 'http://us.battle.net/api/wow/guild/' . $realm . '/' . $guild . '?fields=news';
  
  $ch = curl_init();
  $timeout = 60;
  curl_setopt($ch, CURLOPT_URL, $api_guild_news_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $file_contents= curl_exec($ch);
  curl_close($ch);

  $guildNews = json_decode($file_contents, true);

  echo '<center><font color="#FFD100">Guild Level: ' . $guildNews['level'] . '</font></center><br>';
  if(is_array($guildNews['news']) && count($guildNews['news']) > 0) foreach($guildNews['news'] as $news)
  {
     if($counter++ == $max)
     {
        break;
     }
     switch($news['type'])
     {
        case "itemPurchase" :
        {
           echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' purchased </font>' . ' <a href="http://www.wowhead.com/item=' . $news['itemId'] . '" target="_blank">' . '<font color="'. getColorByItemId($news['itemId']) . '">' . getPropertyByItemId($news['itemId'])  . '</a></font><br>';         
           echo '<div class="widget_post_bit"></div>' ;
           break;
        }
        case "itemLoot" :
        {
           echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' obtained </font>' . ' <a href="http://www.wowhead.com/item=' . $news['itemId'] . '" target="_blank">' . ' <font color="'. getColorByItemId($news['itemId']) . '">' . getPropertyByItemId($news['itemId']) . '</a></font><br>';
           echo '<div class="widget_post_bit"></div>' ;
           break;
        }
        case "playerAchievement" :
        {
           echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' earned the achievement </font>' . '<a href="http://www.wowhead.com/achievement=' . $news['achievement']['id'] . '" target="_blank"><font color="#FFD100"> ' . $news['achievement']['title'] . '</a></font><br>';
           echo '<div class="widget_post_bit"></div>' ;
           break;
        }
        case "guildAchievement" :
        {
           echo '<font color="#F9A205">' . $news['character'] . '</font> <font color="#DEB887">' . ' earned the achievement </font>' . '<a href="http://www.wowhead.com/achievement=' . $news['achievement']['id'] . '" target="_blank"><font color="#FFD100">' . $news['achievement']['title'] . '</a></font><br>';
           echo '<div class="widget_post_bit"></div>' ;
           break;
        }
        case "itemCraft" :
        {
           echo '<font color="#F9A205">' . utf8_array_decode($news['character']) . '</font> <font color="#DEB887">' . ' crafted item </font>' . ' <a href="http://www.wowhead.com/item=' . $news['itemId'] . '" target="_blank">' . '<font color="'. getColorByItemId($news['itemId']) . '">' . getPropertyByItemId($news['itemId']) . '</a></font><br>';
           echo '<div class="widget_post_bit"></div>' ;

           break;
        }
     }
  }

  echo '<br><center><a href="http://us.battle.net/wow/en/guild/' . $realm . '/' . $guild . '/news" target="_blank"><font color="#FFD100">View all!</font></a></center>';

function getPropertyByItemId($id)
{
$api_item_url = “http://” . “us.battle.net” . “/api/wow/item/” . $id;

  $item = json_decode(file_get_contents($api_item_url), true);

  return $item['name'];

}

function getPropertyByAchievementsId($id)
{
$api_item_url = “http://” . “us.battle.net” . “/api/wow/achievements/” . $id;

  $item = json_decode(file_get_contents($api_item_url), true);

  return $item['id'];

}

function getColorByItemId($id)
{
$api_item_url = “http://” . “us.battle.net” . “/api/wow/item/” . $id;

  $item = json_decode(file_get_contents($api_item_url), true);
  
  switch($item['quality'])
  {
     case "4" :
     {
        return "#A335EE";
     }
     case "5" :
     {
        return "#ff8000";
     }
     case "3" :
     {
        return "#0070dd";
     }
  }

}
function utf8_array_decode($input)
{
$return = utf8_decode($input);
return $return;
}
?>
[/php]
I have always found curl to be much more reliable for fetching remote data. file_get_contents can be blocked at the server level and is much slower. This is working consistently for me.

Sponsor our Newsletter | Privacy Policy | Terms of Service