How get a response of multiple price items on the market

家住魔仙堡 提交于 2019-12-19 05:02:02

问题


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.


回答1:


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.




回答2:


I think

  1. populating the data into the JSON
  2. then passing it to the particular page
  3. Get the posted item in the array
  4. now use loop like: foreach and query each id or something in the database and then again store it in the array
  5. now again use loop, if done well step 4 will be the last, to display the related pricing


来源:https://stackoverflow.com/questions/27270881/how-get-a-response-of-multiple-price-items-on-the-market

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!