Class PHP Steam
De Wiki levelKro
Révision datée du 28 janvier 2019 à 04:47 par LevelKro (discussion | contributions)
Steam offre un API mais peu de documentation, voici des codes pour vous aider à inclure Steam dans votre site.
Sommaire
Pré-requis
- Clé d'accès à l'API de Steam.
- PHP 5.4+ avec cURL
SteamCommunity.Class.php
Permet d'avoir les informations;
- D'un groupe
- D'un utilisateur
<?php /* * (c) Copyright Mathieu Légaré (levelkro@yahoo.ca) * 2013-2019 */ class STEAMcommunity { function=nameVAC($id){ switch($id){ case '0': return "En règle"; break; case '1': return "Suspendu"; break; default: return "Non déterminé"; } } function=nameTrade($id) switch($id){ case 'None': return "En règle"; break; case 'Probation': return "Limité"; break; default: return "Non déterminé"; } } function=nameVisibility($id){ switch($id){ case '1': return "Privé"; break; case '2': case '3': return "Public"; break; default: return "Non déterminé"; } } function OpenURL($url){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3"); curl_setopt($ch,CURLOPT_TIMEOUT,1000); $result = curl_exec($ch); return $result; curl_close($ch); } function group($id){ // Return information about a Steam Group $values = self::OpenURL("http://steamcommunity.com/groups/".$id."/memberslistxml/?xml=1"); try{ $values = (array) simplexml_load_string($values); $values['groupDetails'] = (array) $values['groupDetails']; $values['groupDetails']['groupName'] = (string) $values['groupDetails']['groupName']; $values['groupDetails']['groupURL'] = (string) $values['groupDetails']['groupURL']; $values['groupDetails']['headline'] = (string) $values['groupDetails']['headline']; $values['groupDetails']['summary'] = (string) $values['groupDetails']['summary']; $values['groupDetails']['avatarIcon'] = (string) $values['groupDetails']['avatarIcon']; $values['groupDetails']['avatarMedium'] = (string) $values['groupDetails']['avatarMedium']; $values['groupDetails']['avatarFull'] = (string) $values['groupDetails']['avatarFull']; $values['members'] = (array) $values['members']; return $values; } catch (Exception $e) { return false; } } function user($id){ // Return information about a Steam Group $values = self::OpenURL("http://steamcommunity.com/id/".$id."/?xml=1"); try{ $values = (array) simplexml_load_string($values); $values['steamID'] = (string) $values['steamID']; $values['stateMessage'] = (string) $values['stateMessage']; $values['avatarIcon'] = (string) $values['avatarIcon']; $values['avatarMedium'] = (string) $values['avatarMedium']; $values['avatarFull'] = (string) $values['avatarFull']; $values['customURL'] = (string) $values['customURL']; $values['steamRating'] = (string) $values['steamRating']; $values['headline'] = (string) $values['headline']; $values['location'] = (string) $values['location']; $values['realname'] = (string) $values['realname']; $values['summary'] = (string) $values['summary']; $values['mostPlayedGames'] = (array) $values['mostPlayedGames']; $values['mostPlayedGames'] = (array) $values['mostPlayedGames']['mostPlayedGame']; $values['groups'] = (array) $values['groups']; $values['groups'] = (array) $values['groups']['group']; return $values; } catch (Exception $e) { return false; } } function=userAPI($apikey,$value){ $result=$this->remoteUrl("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$apikey."&steamids=".$value."&format=json"); $pro=json_decode($result); return (array) $pro->response->players[0]; } function=userGames($apikey,$steamID){ $json = $this->remoteUrl("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=".$apikey."&steamid=".$steamID."&format=json"); $result = json_decode($json,true); return $result['response']['games']; } function=userBan($apikey,$steamID){ $result = false; $json = $this->remoteUrl("http://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=".$apikey."&steamids=".$steamID."&format=json"); $array = json_decode($json,true); return $array['players']['0']; } }
SteamMarket.Class.php
Permet d'avoir les informations;
- Nombre total de produits
- Résultat depuis 'X' des items (100 éléments maximum)
- Détails d'un produit du marché spécifique (avec URL)
<?php /* * (c) Copyright Mathieu Légaré (levelkro@yahoo.ca) * 2013-2019 */ class STEAMmarket { // Récupérer le contenu d'une page distant function getUrlContent($url){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3"); curl_setopt($ch,CURLOPT_TIMEOUT,1000); $result = curl_exec($ch); return $result; curl_close($ch); } // Récupère la listes des 100 éléments suivants function getListDetails($s=0){ $l="https://steamcommunity.com/market/search/render/?query=&start=".$s."&count=100&norender=1"; $s=getUrlContent($l); $n=(array) json_decode($s, true); return $n; } // Récupère le nombre d'éléments à importer function getListPages(){ $l="https://steamcommunity.com/market/search/render/?query=&start=0&count=100&norender=1"; $s=getUrlContent($l); $n=(array) json_decode($s, true); return $n['total_count']; } function getInfos($p){ // Get informations about the item in Market $source = self::OpenURL($p); $htmlsrc=explode("<!-- main body -->",$source); // The beginning $htmlsrc=explode("<div id=\"market_activity_section\">",$htmlsrc[1]); // The end of part of code $htmlsrc=$htmlsrc[0]; $htmlsrc=str_replace("&"."#36;","",$htmlsrc); $htmlsrc=str_replace("\r","",$htmlsrc); $htmlsrc=str_replace("\t","",$htmlsrc); $htmlsrc=str_replace("\n","",$htmlsrc); $htmlsrc=str_replace(">","",$htmlsrc); $htmlsrc=str_replace("<div id=\"BG_bottom\"><div id=\"mainContents\"><div class=\"market_listing_nav_container\"><div class=\"market_listing_nav\">","<item>\r\n",$htmlsrc); $htmlsrc=str_replace("<div style=\"clear: both;\"></div>","",$htmlsrc); $htmlsrc=preg_replace("/<\/a>/i","</a>\r\n", $htmlsrc); $htmlsrc=explode("<div id=\"largeiteminfo\">",$htmlsrc); $htmlsrc=$htmlsrc[0]; $htmlsrc=str_replace("</div></div><div class=\"market_page_fullwidth market_listing_firstsection\"><div class=\"market_listing_iteminfo\"><div class=\"market_listing_largeimage\">","",$htmlsrc); $htmlsrc=preg_replace("/<a href=\"(.+)search(.+)=(.+)\">(.+)<\/a>/i","<catAppid>$3</catAppid>\r\n<catName>$4</catName>", $htmlsrc); $htmlsrc=preg_replace("/<a href=\"(.+)listing(.+)\">(.+)<\/a>/i","<itemUrl>$1listing$2</itemUrl>\r\n<itemName>$3</itemName>", $htmlsrc); $htmlsrc=preg_replace("/<img src=\"(.+)\" alt=\"\" \/><\/div>/i","<itemImage>$1</itemImage>", $htmlsrc); $htmlsrc=$htmlsrc."</item>"; $htmlsrc="<?phpxml version=\"1.0\" encoding=\"UTF-8\"?>\r\n".$htmlsrc.""; $htmlsrc=str_replace("&","&",$htmlsrc); try{ $htmlsrc = simplexml_load_string($htmlsrc); $htmlsrc = (array) $htmlsrc; } catch (Exception $e) { $htmlsrc=array(); } $jsonAsrc=explode("var g_rgAppContextData = ",$source); $jsonAsrc=explode("var g_rgAssets",$jsonAsrc[1]); $jsonAsrc=explode("\"".$htmlsrc['catAppid']."\":",$jsonAsrc[0]); $jsonAsrc=explode("};",$jsonAsrc[1]); if(strpos($jsonAsrc[0],"},")){ $jsonAsrc=explode("},",$jsonAsrc[0]); $jsonAsrc=explode(":{",$jsonAsrc[1]); $jsonAsrc=(array) json_decode("{".$jsonAsrc[1],true); } else{ $jsonAsrc=(array) json_decode($jsonAsrc[0],true); } $jsonBsrc=explode("var g_rgAssets = ",$source); $jsonBsrc=explode("var g_rgListingInfo",$jsonBsrc[1]); $jsonBsrc=explode("\"".$htmlsrc['catAppid']."\":",$jsonBsrc[0]); $jsonBsrc=explode("};",$jsonBsrc[1]); $jsonBsrc=(array) json_decode($jsonBsrc[0],true); foreach($jsonBsrc as $jsonItemsrc){ $jsonBsrc=$jsonItemsrc; } $o=1; foreach($jsonBsrc as $jsonItemsrc){ if($o==1){ $jsonItem=$jsonItemsrc; $o++; } } $jsonItemDesc=""; foreach($jsonItem['descriptions'] as $jsonDescsrc){ $jsonItemDesc.=$jsonDescsrc['value']."<br/>"; } $jsonItem['descriptions']=$jsonItemDesc; $output=array(); $output['base']=$htmlsrc; $output['parent']=$jsonAsrc; $output['item']=$jsonItem; return $output; } }
SteamStore.Class.php
Permet d'avoir les informations;
- De tous les produits disponibles (sans les exclus, désactivés, supprimés, etc...)
- De tous les packages associés aux produits
- D'un produit spécifique
<?php /* * (c) Copyright Mathieu Légaré (levelkro@yahoo.ca) * 2013-2019 */ class STEAMstore { }
SteamLogin.Class.php
Requis pour l'identification avec Steam, utilise le OpenID Auth 2.0.
<?php /* PHP Steam Login */ class STEAMSignIn { const STEAM_LOGIN = 'https://steamcommunity.com/openid/login'; public static function genUrl($returnTo = false, $useAmp = true) { $returnTo = (!$returnTo) ? (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] : $returnTo; $params = array('openid.ns'=>'http://specs.openid.net/auth/2.0','openid.mode'=>'checkid_setup','openid.return_to'=> $returnTo,'openid.realm'=>(!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'],'openid.identity'=>'http://specs.openid.net/auth/2.0/identifier_select','openid.claimed_id'=>'http://specs.openid.net/auth/2.0/identifier_select'); $sep = ($useAmp) ? '&' : '&'; return self::STEAM_LOGIN . '?' . http_build_query($params, '', $sep); } public static function validate() { $params = array('openid.assoc_handle'=>$_GET['openid_assoc_handle'],'openid.signed'=>$_GET['openid_signed'],'openid.sig'=>$_GET['openid_sig'],'openid.ns'=>'http://specs.openid.net/auth/2.0'); $signed = explode(',', $_GET['openid_signed']); foreach($signed as $item) { $val = $_GET['openid_' . str_replace('.', '_', $item)]; $params['openid.' . $item] = get_magic_quotes_gpc() ? stripslashes($val) : $val; } $params['openid.mode'] = 'check_authentication'; $data = http_build_query($params); $context = stream_context_create(array('http' => array('method'=>'POST','header'=>"Accept-language: en\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($data)."\r\n",'content'=>$data))); $result = file_get_contents(self::STEAM_LOGIN, false, $context); preg_match("#^https://steamcommunity.com/openid/id/([0-9]{17,25})#", $_GET['openid_claimed_id'], $matches); $steamid64 = is_numeric($matches[1]) ? $matches[1] : 0; return preg_match("#is_valid\s*:\s*true#i", $result) == 1 ? $steamid64 : false; } }