问题描述
我正在尝试使用PHP获取某项的Steam社区市场价格.我输入了一个网址(例如: http://steamcommunity.com/market/listings/730/StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29 ),然后用file_get_contents()
下载内容.我试图用这个:
function getInnerHTML($string, $tagname, $closetagname) {
$pattern = "/<$tagname ?.*>(.*)<\/$closetagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
使用
getInnerHTML($str, 'span class="market_listing_price market_listing_price_with_fee"', 'span');
关于file_get_contents的示例如下:
<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>
但是它什么也不返回.
有人有主意吗?
不完全确定为什么当要返回JSON的情况下可以通过HTML进行正则表达式时,为什么要用这种困难的方式进行正则表达式.尽管原始答案是正确的,并且可以直接回答OP问题,但这提供了一种获取商品市场价值的简便而有效的方法.
获取:
JSON响应:
{
"success": true,
"lowest_price": "1,43€ ",
"volume": "562",
"median_price": "1,60€ "
}
响应定义:
success
:布尔值,如果调用成功,则为true;如果出现错误,则为false.
lowest_price
:带有货币符号[pre//ap]的字符串值取决于指定的查询参数.有关其他一些参数,请参见下文.
volume
:作为字符串(?)返回的整数值-已售出/购买的此特定商品的总数.
median_price
:带有货币符号[pre-/ap]的字符串值.该商品的平均售价.请参见蒸汽市场项目图,以更好地了解中位数的计算方式.
查询参数:
appid
:游戏/应用程序的唯一(静态定义)Steam应用程序ID,在我们的案例中为730:《反恐精英:全球攻势》.有关其他appid的列表,请参见Valve的开发Wiki,尽管随着新应用频繁添加到其平台上,该列表很可能总是过时.
market_hash_name
:包含外部外观的要查询的项目名称,在查询用户清单时可以找到这些名称,但这是另一个完整的API调用.
currency
:一个整数值;货币价值和返回市场价值的格式.您需要调整并使用这些数字,因为我在这里不能提供太多细节.通常,我坚持使用美元作为全球价格,并使用自己的货币API转换为其他货币.
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 ?
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:
JSON Response:
{
"success": true,
"lowest_price": "1,43€ ",
"volume": "562",
"median_price": "1,60€ "
}
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.
这篇关于使用PHP和Regex获取Steam社区市场上的商品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!