Class PHP Steam

De Wiki levelKro
Révision datée du 25 janvier 2019 à 05:47 par LevelKro (discussion | contributions) (Page créée avec « Steam offre un API mais peu de documentation, voici des codes pour vous aider à inclure Steam dans votre site. == Pré-requis == * Clé d'accès à l'API de Steam. * PHP... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

Steam offre un API mais peu de documentation, voici des codes pour vous aider à inclure Steam dans votre site.

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 OpenURL($url){
			// Code for get content of remote 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;
			} 		
		}			
	}	

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