简体   繁体   English

CoinMarketCap Api 调用元数据

[英]CoinMarketCap Api Call Metadata

i'm working with the CoinMarketCap Api and trying to fetch the Metadata API, but still searching for the right path to output Logo.我正在使用 CoinMarketCap Api 并尝试获取元数据 API,但仍在寻找输出徽标的正确路径。

$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/info";

$headers = [
  'Accepts: application/json',
  'X-CMC_PRO_API_KEY: key'
];

$request = "{$url}"; // create the request URL

$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
  CURLOPT_URL => $request,            // set the request URL
  CURLOPT_HTTPHEADER => $headers,     // set the headers 
  CURLOPT_RETURNTRANSFER => 1         // ask for raw response instead of bool
));

$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request

$logo = $json->data[array_search('bitcoin', array_column($json->data, 'slug'))]->logo;

echo $logo;

Can someone see what is wrong here?有人能看到这里有什么问题吗?

https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyInfo https://coinmarketcap.com/api/documentation/v1/#operation/getV1CryptocurrencyInfo

You are missing the slug value in the curl request, and this value is required for the cryptocurrency/info endpoint.您在 curl 请求中缺少 slug 值,并且加密货币/信息端点需要此值。 Below are two working examples of what you are trying to achieve, but in python and curl respectively so they can be easily run in the command line or REPL:以下是您尝试实现的两个工作示例,但分别在 python 和 curl 中,因此它们可以轻松地在命令行或 REPL 中运行:

Curl:卷曲:

curl -H "X-CMC_PRO_API_KEY: your-key-here" -H "Accept: application/json" -d "slug=bitcoin" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/info

Python: Python:

from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json

url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/info'
parameters = {
  'slug':'bitcoin'
}
headers = {
  'Accepts': 'application/json',
  'X-CMC_PRO_API_KEY': 'your-key-here',
}

session = Session()
session.headers.update(headers)

try:
  response = session.get(url, params=parameters)
  data = json.loads(response.text)
  print(data)
except (ConnectionError, Timeout, TooManyRedirects) as e:
  print(e)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM