Found your problem 🙂
Reason:
Your script was getting halted while downloading xml file from "http://steamcommunity.com/profiles/76561197966374919?xml=1"
This xml has some weird characters, may be non ASCII, non printable or whatever.
Solution:
The thing is you should download files with some timeout set (especially in loops). Skip the iteration where time taken is unacceptable.
Check set time out while downloading files to know about different ways of setting timeout options in PHP.
I modified your code, update it wherever need be,
<?php
$friendslist = "http://steamcommunity.com/profiles/76561197970734089/friends/?xml=1" ;
if(!$xml_str = check_url($friendslist))
{
echo "XML file not found";
exit;
}
if(! $xml = simplexml_load_string($xml_str) )
{
echo "File not loaded";
exit;
}
$steamID = $xml->steamID ;
echo "<h1>Main Profile: " . $steamID . "</h1>";
$count = 1;
$limit = 100;
foreach ($xml->friends->children() as $friendid)
{
if($count>$limit) // more friends more laziness :)
break;
$count++;
if( ! isclean($friendid) ) // clean it since it is kind of user input
{
echo "Invalid id for child $friendget ";
continue;
}
$friendget = "http://steamcommunity.com/profiles/".$friendid."?xml=1" ;
$xml_str = ""; //empty it for every iteration
if(!$xml_str = check_url($friendget))
{
echo "XML file not found for child $friendget ";
continue;
}
if(! $friendgot = @simplexml_load_string($xml_str) )
{
echo "File not loaded for child $friendget ";
continue;
}
echo $friendgot->steamID ;
echo "<br />" ;
}
///////////////////////////////////////////////////////////////////////////////////////////
function isclean($friendid) //make it your way
{
echo "checking |$friendid| ", is_int($friendid);
if( is_int($friendid) || is_float($friendid) ) //is_int() alone fails. Limited to the word size of the environment php is running in
return true;
else
return false;
}
function check_url($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HEADER, 0); // dont get the header
//curl_setopt($c, CURLOPT_BINARYTRANSFER, false); // TRUE to return the raw output when CURLOPT_RETURNTRANSFER is used.
//curl_setopt($c, CURLOPT_TRANSFERTEXT, true); // TRUE to use ASCII mode for FTP transfers. For LDAP, it retrieves data in plain text instead of HTML. On Windows systems, it will not set STDOUT to binary mode.
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // to get the result as a string
curl_setopt($c, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 1); //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
curl_setopt($c, CURLOPT_TIMEOUT, 2); //The maximum number of seconds to allow cURL functions to execute.
if (!$xml = curl_exec($c))
{
$errmsg = curl_error( $c );
echo "Error: cURL $errmsg <br>";
return ;
}
return $xml;
}
?>