OzBarry
OzBarry

Reputation: 1198

PHP file_get_contents() passing information via GET

I'm writing a PHP script that retrieves steam user profile information (for a servers forum/website I'm making), and I seem to be stuck on getting the xml file. To get the data in xml format, the address is http://steamcommunity.com/profiles/<steam 64 id>/?xml=1

The issue is that it doesn't seem to actually be passing the ?xml=1 as GET data, but instead, possibly ignoring it. Here is by code:

function parseSteamInformation( $steamid ) {
    $result = file_get_contents( "http://steamcommunity.com/profiles/$steamid/?xml=1" );
    $xml = simplexml_load_string( $result );
    if( $xml ) {
        $data = array( 
                    "id64" => $xml->steamID64,
                    "steamName" => $xml->steamID,
                    "onlineState" => $xml->onlineState,
                    "stateMessage" => $xml->stateMessage,
                    "avatarIcon" => $xml->avatarFull,
                    "vacBanned" => $xml->vacBanned,
                    "steamRating" => $xml->steamRating,
                    "realName" => $xml->realName,
                );
        return $data;
    }
    return false;
}

The result string is actually HTML instead of XML, but according to https://partner.steamgames.com/documentation/community_data , adding ?xml=1 should output XML.

I looked at content streaming, and tried adding:

$data =  http_build_query(array("xml"=>1));  // needed somewhere?
$context = stream_context_create(array(
    'http' => array(
        'method' => 'GET',
        'header' => "Accept-language: en\r\n" .
                    "Cookie: xml=1\r\n"
    )
));
$result = file_get_contents( "http://steamcommunity.com/profiles/${steamid}/", false, $context );

Because google didn't reveal much information on passing GET parameters to a website with file _get_contents(), I'm assuming I'm over-complicating something.

Any help is appreciated, -Oz

Upvotes: 1

Views: 1163

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

alternative solution, could be slower then others n most prob get slated for some reason but hope it helps :/

<?php
function strip_cdata($string)
{
    preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
    return str_replace($matches[0], $matches[1], $string);
} 

function get_steam($profile){
    //make steam think its a browser
    ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
    ini_set('default_socket_timeout',5);
    $return ='';
    if(isset($profile) && is_numeric($profile)){
        $spg=file_get_contents('http://steamcommunity.com/profiles/'.$profile.'/?xml=1');
        if($spg==true){
            preg_match('/<steamID64>(.*)<\/steamID64>/i',$spg,$matchSID);
            preg_match('/<steamID>(.*)<\/steamID>/i',$spg,$matchSName);
            preg_match('/<onlineState>(.*)<\/onlineState>/i',$spg,$matchSStatus);
            preg_match('/<stateMessage>(.*)<\/stateMessage>/i',$spg,$matchSLastOnline);
            preg_match('/<vacBanned>(.*)<\/vacBanned>/i',$spg,$matchSBanned);
            preg_match('/<avatarFull>(.*)<\/avatarFull>/i',$spg,$matchSAvatar);
            preg_match('/<memberSince>(.*)<\/memberSince>/i',$spg,$matchSSignUp);
            preg_match('/<steamRating>(.*)<\/steamRating>/i',$spg,$matchSRating);
            preg_match('/<location>(.*)<\/location>/i',$spg,$matchSLocation);
            preg_match('/<realname>(.*)<\/realname>/i',$spg,$matchSRealName);
            preg_match('/<summary>(.*)<\/summary>/i',$spg,$matchSummary);

            $data = array(
            "id64" => $matchSID[1],
            "steamName" => strip_cdata($matchSName[1]),
            "onlineState" => strip_cdata($matchSStatus[1]),
            "stateMessage" => strip_cdata($matchSLastOnline[1]),
            "avatarIcon" => strip_cdata($matchSAvatar[1]),
            "memberSince" => $matchSSignUp[1],
            "vacBanned" => $matchSBanned[1],
            "steamRating" => $matchSRating[1],
            "steamLocation" => strip_cdata($matchSLocation[1]),
            "realName" => strip_cdata($matchSRealName[1]),
            "summary" => strip_cdata($matchSummary[1])
            );
            return $data;
        }else{
            return "E2";}
    }else{return "E1";}
}

//do the function
$steamData=get_steam('76561197968575517');
//check the results
if($steamData=='E1'){
    echo'Profile ID not set.';
}elseif($steamData=='E2'){
    echo'Error Connecting to Steam Profile.';
}else{
   //output example
    echo '<pre>';
    foreach($steamData as $key => $value){
        echo ''.$key.'-'.$value.'<br>';
    }
    echo'</pre>';
}
?>

Upvotes: 0

alex
alex

Reputation: 490233

I don't think it is the GET param, as this worked fine for me...

function parseSteamInformation( $steamid ) {
    $result = file_get_contents( "http://steamcommunity.com/profiles/$steamid/?xml=1" );
    $xml = simplexml_load_string( $result );
    if( $xml ) {
        $data = array(
                    "id64" => $xml->steamID64,
                    "steamName" => $xml->steamID,
                    "onlineState" => $xml->onlineState,
                    "stateMessage" => $xml->stateMessage,
                    "avatarIcon" => $xml->avatarFull,
                    "vacBanned" => $xml->vacBanned,
                    "steamRating" => $xml->steamRating,
                    "realName" => $xml->realName,
                );
        return $data;
    }
    return false;
}

var_dump(parseSteamInformation('76561197968575517'));

Output

array(8) {
  ["id64"]=>
  object(SimpleXMLElement)#16 (1) {
    [0]=>
    string(17) "76561197968575517"
  }
  ["steamName"]=>
  object(SimpleXMLElement)#17 (0) {
  }
  ["onlineState"]=>
  object(SimpleXMLElement)#18 (1) {
    [0]=>
    string(7) "offline"
  }
  ["stateMessage"]=>
  object(SimpleXMLElement)#19 (0) {
  }
  ["avatarIcon"]=>
  object(SimpleXMLElement)#20 (0) {
  }
  ["vacBanned"]=>
  object(SimpleXMLElement)#21 (1) {
    [0]=>
    string(1) "0"
  }
  ["steamRating"]=>
  object(SimpleXMLElement)#22 (1) {
    [0]=>
    string(1) "0"
  }
  ["realName"]=>
  object(SimpleXMLElement)#23 (0) {
  }
}

Upvotes: 2

Related Questions