PHP file_get_contents()通过GET传递信息

时间:2011-03-16 03:26:36

标签: php get file-get-contents

我正在编写一个PHP脚本来检索Steam用户配置文件信息(对于我正在制作的服务器论坛/网站),我似乎一直在寻找xml文件。要以xml格式获取数据,地址为http://steamcommunity.com/profiles/<steam 64 id>/?xml=1

问题在于它似乎并没有将?xml = 1作为GET数据传递,而是可能忽略它。这是代码:

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;
}

结果字符串实际上是HTML而不是XML,但根据https://partner.steamgames.com/documentation/community_data,添加?xml = 1应该输出XML。

我查看了内容流,并尝试添加:

$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 );

因为谷歌没有透露有关将GET参数传递到带有文件的网站的大量信息 _get_contents(),我假设我过于复杂了。

感谢任何帮助, -Oz

2 个答案:

答案 0 :(得分:2)

我认为这不是GET参数,因为这对我来说很好......

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'));

输出

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) {
  }
}

答案 1 :(得分:0)

替代解决方案,可能比其他人慢,但是由于某些原因,大多数问题得到了解决,但希望它有所帮助:/

<?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>';
}
?>