来自 Steam 社区市场的卷曲数据

Curl data from Steam community market

我一直在尝试从 Steam 社区市场获取数据, 代码:

#include <iostream>

#include <string>
#include <curl/curl.h>

static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(void)
{
    CURL* curl;
    CURLcode res;
    std::string readBuffer;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://steamcommunity.com/market/priceoverview/?market_hash_name=AK-47%20%7C%20Redline%20%28Field-Tested%29&appid=730&currency=1");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

        std::cout << readBuffer << std::endl;
    }
    return 0;
}

但是当我 运行 它时,没有任何显示。 任何帮助表示赞赏, 谢谢

curl 默认情况下不遵循重定向,而您提到的网站使用了这些重定向。

我必须打开 CURLOPT_FOLLOWLOCATION 才能让它工作:

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);  // redirects
// bonus:
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L); // corp. proxies etc.

可能的输出:

{"success":true,"lowest_price":".00","volume":"477","median_price":".95"}

调试时,您可能也需要此选项以查看 curl 的目的:

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);