如何获得市场上多个价格项目的响应 [英] How get a response of multiple price items on the market

查看:122
本文介绍了如何获得市场上多个价格项目的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过此链接,我检查了背包cs:go中各项物品的价格:

http://steamcommunity.com/市场/价格概述/?country = FR& currency = 3& appid = 440& market_hash_name =

但是对于100个项目,例如,我检查了100个链接以获取我所有项目的价格.

是否可以查询包含很多物品的Steam,并且Steam响应仅请求所有价格的一个json?

我希望它是一个像这样的系统,您将包含要知道价格的所有商品的所有classid的数组发送到Steam网址,然后Steam将包含数组所有价格的json发送给您.对于Steam来说,这并不困难,而且非常快速,对我而言,这对于查询的速度和更简单而言是非常有帮助的.

解决方案

是否可以查询包含很多物品的Steam,并且Steam响应仅请求所有价格的一个json?

否,不可能用很多项目来查询Steam,这会导致对所有数据的单个响应,但是可以使用cURL的multiget发送多个请求,每个请求均会击中Steam API,并在单个cURL调用中返回多个响应

curl多执行程序的示例:

function curl_get_contents($data) {
    $curly = array();
    $mh = curl_multi_init();

    foreach ($data as $urlArray) {
        $curly[$urlArray['name']] = curl_init();
        curl_setopt($curly[$urlArray['name']], CURLOPT_URL, $urlArray['url']);
        curl_setopt($curly[$urlArray['name']], CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curly[$urlArray['name']], CURLOPT_HEADER, false);
        curl_setopt($curly[$urlArray['name']], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($mh, $curly[$urlArray['name']]);
    }

    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while ($running > 0);

    foreach ($curly as $id => $c) {
        curl_multi_remove_handle($mh, $c);
    }

    curl_multi_close($mh);
 }

我写了一个类似这样的函数(删除了额外的代码,因为我不想让您只是复制/粘贴-学习)来获取一个数组数组,其中每个内部数组都包含一个名称和一个URL,并且相应地返回数据数组-例如:

$curlyUrls = array(
        array(
        'name' => 'Item1 Response',
        'url'  => 'http://steamcommunity.com/market/priceoverview/?country=ZA&currency=3&appid=730&market_hash_name=Item1'
        ),
        array(
        'name' => 'Item2 Response',
        'url'  => 'http://steamcommunity.com/market/priceoverview/?country=ZA&currency=3&appid=730&market_hash_name=Item2'
        )
);

$curlyItemResponse = curl_get_contents($curlyUrls);

绝对不建议对100多个项目使用此方法.我最多使用10次此操作-太频繁地使用Steam API可能会导致某种形式的连接节流(更不用说了),如果调用频率太高,则每天100K API阈值将很快用完. /p>

有一些解决方法,但是我推荐的最佳方法是在数据库中存储您已知物品的列表,并创建一个cronjob来偶尔更新每个物品的价格,同时避免大量API调用-这样,您每个商品都有一个缓存的价格.

I check the price of each items of my backpack cs:go with this link :

http://steamcommunity.com/market/priceoverview/?country=FR&currency=3&appid=440&market_hash_name=

But for 100 items for exemple, i check 100 links for get the price of all my items.

Is it possible to query steam with many items and steam response only one json with all prices requested?

I want it's a system like that, you send a array with all classid of the items you want know the price to a steam url and steam send you one json with all price of your array. For steam it's not difficult and it's very speedy and for me it's very helpul for the speed of query and easier.

解决方案

Is it possible to query steam with many items and steam response only one json with all prices requested?

No, it's not possible to query Steam with many items which result in a single response with all the data, however it's possible to use cURL's multiget to send multiple requests which each hit the Steam API returning multiple response in a single cURL call.

Example of a curl multi exec:

function curl_get_contents($data) {
    $curly = array();
    $mh = curl_multi_init();

    foreach ($data as $urlArray) {
        $curly[$urlArray['name']] = curl_init();
        curl_setopt($curly[$urlArray['name']], CURLOPT_URL, $urlArray['url']);
        curl_setopt($curly[$urlArray['name']], CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curly[$urlArray['name']], CURLOPT_HEADER, false);
        curl_setopt($curly[$urlArray['name']], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($mh, $curly[$urlArray['name']]);
    }

    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while ($running > 0);

    foreach ($curly as $id => $c) {
        curl_multi_remove_handle($mh, $c);
    }

    curl_multi_close($mh);
 }

I wrote a function much like this one (removed the additional code because I don't want you to just copy/pasta - learn instead) to take an array of arrays where each inner array contains a name and a URL, and returns an array of data accordingly - for example:

$curlyUrls = array(
        array(
        'name' => 'Item1 Response',
        'url'  => 'http://steamcommunity.com/market/priceoverview/?country=ZA&currency=3&appid=730&market_hash_name=Item1'
        ),
        array(
        'name' => 'Item2 Response',
        'url'  => 'http://steamcommunity.com/market/priceoverview/?country=ZA&currency=3&appid=730&market_hash_name=Item2'
        )
);

$curlyItemResponse = curl_get_contents($curlyUrls);

This method is definitely not recommended for 100+ items. I use this for no more than 10 calls - hitting the Steam API too frequently will likely cause some sort of throttling toward your connection not to mentioned, if this is called too frequently, your 100K daily API threshold will be used up pretty fast.

There are a few workarounds to this, but the best approach I can recommend is storing a list of your known items in a database and creating a cronjob to occasionally update the prices of each item while avoiding mass API calls - that way you have a cached price to each item.

这篇关于如何获得市场上多个价格项目的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆