Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...