Get the price of an item on Steam Community Market with PHP and Regex
Asked Answered
C

3

15

I'm trying to use PHP to get the Steam Community Market price of an item. I take a url (for example : http://steamcommunity.com/market/listings/730/StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29) and then I download the content with file_get_contents(). I tried to use this :

function getInnerHTML($string, $tagname, $closetagname) {
    $pattern = "/<$tagname ?.*>(.*)<\/$closetagname>/";
    preg_match($pattern, $string, $matches);
    return $matches[1];
}

Using

getInnerHTML($str, 'span class="market_listing_price market_listing_price_with_fee"', 'span');

An example of what I can have with file_get_contents is this :

<span class="market_table_value">
    <span class="market_listing_price market_listing_price_with_fee">
        $1.92               </span>
    <span class="market_listing_price market_listing_price_without_fee">
        $1.68               </span>
    <br/>
</span>

But it returns nothing.

Has anyone an idea ?

Cisco answered 14/4, 2014 at 8:44 Comment(5)
The fact that you're using .*>(.*) in your expression instead of a non-greedy pattern and by forgetting the s modifier. It's clear that you need to stop using regex and use a robust html parserHeathenish
You shouldn't use regex for this task, try using PHP's DOM instead: https://mcmap.net/q/350222/-php-parse-html-code-duplicateScurlock
I tried the PHP's DOM but I can't use it because each item in the list has in the span the title attribute, which is different for each item and the title is the price. So I can't know it.Cisco
@ThomasPatKowalski-Zuckerberg: I'm not sure to follow what you mean. Can you edit your question to add sample input?Scurlock
OK I just added a sample HTML code.Cisco
S
42

Not entirely sure why you'd want to do this the hard way and regex through HTML when there's a perfectly working call which returns JSON. Although the original answer is correct and answers the OP question directly, this provides a much easier and efficient way of getting the market value of an item.

GET:

http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29

JSON Response:

{
  "success": true,
  "lowest_price": "1,43&#8364; ",
  "volume": "562",
  "median_price": "1,60&#8364; "
}

Response Definitions :

success: boolean value, true if the call was successful or false if something went wrong or there are no listing for this item on the Steam market.

lowest_price: string value with currency symbol [pre-/ap]pended depending on the query parameters specified. See below for some additional parameter.

volume: integer value returned as a string (?) - the total number of this specific item which has been sold/bought.

median_price: string value with currency symbol [pre-/ap]pended. The average price at which the item has been sold. See the Steam marketplace item graph for a better understanding on how the median is calculated.

Query Parameters:

appid: The unique (statically defined) Steam application ID of the game/app, in our case 730 for Counter-Strike: Global Offensive. See Valve's development Wiki for a list of other appid's, though this list will most probably always be out of date as new apps are added to their platform frequently.

market_hash_name: The name of the item being queried against with the exterior included, retrieving these names can be found when querying against a users inventory, but that's a whole other API call.

currency: An integer value; the currency value and format to return the market values. You'll need to tweak and play around with these numbers as I cannot provide too much detail here. Generally I stick to using USD as a global price and use my own currency API to translate into other currencies.

This is an undocumented endpoint and therefore might not be permanent, or may be subject to change, nobody knows.

Saideman answered 4/9, 2014 at 12:36 Comment(9)
Is there a way to get the pricing of multiple items rather than sending one GET request per item for the prices?Veliger
No, but see my answer to the same question here: #27271381Saideman
@snh Hey can u please tell me what i this median price because few items have them and few dontOversubscribe
I've updated my answer with a little more information.Saideman
@Stevovosaurus, what is the currency value for usd? currency 3 is returning to me euro dollars.Viperine
I believe 1 is USD.Saideman
Currently: 1 = USD; 13 = CAD; 4 = CHF; 7 = R$; 9 = kr;Tver
You'll run into problems with error 429 if you try to use this for all your inventory items. Because you'll make to many requests too fast.Marchioness
@Marchioness Caching. Nuff said.Saideman
S
4

Don't use regex for this task (see RegEx match open tags except XHTML self-contained tags, but there's a more explanatory link somewhere on SO)

You want to use XPath to select your elements based on fine criteria. From PHP.net this should get you the nodes you want:

$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);

$elements = $xpath->query('//span[@class="market_listing_price market_listing_price_with_fee"]');

the XPath //span[@class="..."] means select all span tags within the document the have the expected class attribute.

Scurlock answered 14/4, 2014 at 9:18 Comment(2)
@Scurlock I'm trying something similar to OP. The data I wish to parse pastebin.com/1J2syC0n . Is it possible to use XPath on such a string of text. Or do I need to make it into a type of file before I can run the query on it?Whisenhunt
@Ríomhaire: if you have another question, you'll have more support by posting it as such. But before that I suggest you try it out to figure what's working as expected and what's not, in order to clarify your exact issue !Scurlock
N
0

I created a node.js module via npm for the cs:go market. https://www.npmjs.com/package/csgo-market It only gets single prices at the moment, but let me know if there is additional functionality you'd like me to add.

Nela answered 23/7, 2015 at 4:24 Comment(1)
Man this is so cool :o if only I knew anything about nodejs... :cCisco

© 2022 - 2024 — McMap. All rights reserved.