我有这个函数,应该从下一页中提取汇率并将其作为值返回。问题是由于某种原因我不知道它无法捕获该值。
double getExchangeRate()
{
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
double exchangeRate = 0.0;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://open.er-api.com/v6/latest/USD");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// Make the request
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() falue: %s\n", curl_easy_strerror(res));
}
else
{
printf("Response length: %zu\n", chunk.size);
printf("API response: %s\n", chunk.memory);
// Find the UYU exchange rate in the answer
char *start = strstr(chunk.memory, "\"UYU\":");
if (start)
{
start += 6; // Move the pointer after "\"UYU\":"
exchangeRate = strtod(start, &start); //
printf("Exchange rate obtained: %.2f\n", exchangeRate);
}
else
{
printf("The UYU exchange rate was not found in the response.\n");
}
}
free(chunk.memory);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return exchangeRate;
}
因为您只显示了代码的一部分...这是一个检索和扫描速率的简单示例,如果找到则打印
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <curl/curl.h>
char resultsBuff[8192*4] = {0}; // Buffer for response, populated in the callback fn
size_t callbackWr(void *contents, size_t size, size_t nmemb, void *userp) {
size_t total_size = size * nmemb;
char *buffer = (char *)userp;
strncat(buffer, (char *)contents, total_size);
return total_size;
}
void scanResults( char *curr )
{
char currBuff[80];
char *start;
char *endOf;
char *rates;
char thisRate[80];
rates = strstr( resultsBuff, "\"rates\":" );//scan only the 'rates' bit
if ( rates == NULL )
{
fprintf(stderr, "no rates found in [%s]\n", resultsBuff );
exit(1);
}
sprintf(currBuff, "\"%s\"", curr );
start = strstr(rates, currBuff);
if ( start )
{
endOf=strstr(start, "," );
if ( endOf == NULL )
endOf=strstr(start, "}" ); // if its the last currency ...
if ( endOf != NULL )
strncpy( thisRate, start, endOf - start );
}
if ( start == NULL || endOf == NULL )
fprintf( stderr, "no exchange rate found for [%s]\n", curr );
else
printf( "%s\n", thisRate );
}
int main( int argc, char **argv )
{
char currency[20];
CURL *curl;
CURLcode res;
if ( argc == 2 )
strcpy( currency, argv[1] );
else{
printf( "usage: %s currency\n", argv[0] );
exit(1);
}
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://open.er-api.com/v6/latest/USD");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callbackWr );
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resultsBuff);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "Request failed: %s\n", curl_easy_strerror(res));
else
scanResults( currency ); // print if a rate exists for this currency
curl_easy_cleanup(curl);
}
return 0;
}
# running that against the following
for rate in XDF XPF YER ZAR USD GBP YEN JPY BSD USD AZN GIP HKD
do
./curly $rate
done
no exchange rate found for [XDF]
"XPF":113.645236
"YER":249.782528
"ZAR":18.148279
"USD":1
"GBP":0.789701
no exchange rate found for [YEN]
"JPY":149.715109
"BSD":1
"USD":1
"AZN":1.700218
"GIP":0.789699
"HKD":7.781721