How get a response of multiple price items on the market

五迷三道 提交于 2019-12-01 02:02:01

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.

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