Issue
I've been trying to fetch data from steam community market , Code :
#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¤cy=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;
}
But when I run it , nothing show up . Any help is appreciated , Thanks
Solution
curl
doesn't follow redirects by default, and the site you mention uses those.
I had to turn on CURLOPT_FOLLOWLOCATION
to make it work:
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // redirects
// bonus:
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L); // corp. proxies etc.
Possible output:
{"success":true,"lowest_price":"$19.00","volume":"477","median_price":"$18.95"}
While debugging, you may want this option too to see what curl
is up to:
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
Answered By - Ted Lyngmo