coinmarketcap新api将Node.js转换为php

时间:2019-02-03 16:52:39

标签: javascript php api

我正在使用他们的API从coinmarketcap.com获取数据。他们本周更新了api,因此现在您需要一个api密钥才能访问数据。 现在,我需要向http调用添加更多详细信息。在API文档中,他们显示了此NodeJs代码:

/* Example in Node.js ES6 using request-promise, concepts should translate to your language of choice */

const rp = require('request-promise');
const requestOptions = {
  method: 'GET',
  uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
  qs: {
    start: 1,
    limit: 5000,
    convert: 'USD'
  },
  headers: {
    'X-CMC_PRO_API_KEY': 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c'
  },
  json: true,
  gzip: true
};

rp(requestOptions).then(response => {
  console.log('API call response:', response);
}).catch((err) => {
  console.log('API call error:', err.message);
});

有人可以帮我将所附的nodejs代码转换为php吗?

1 个答案:

答案 0 :(得分:0)

我认为,如果您一直使用PHP与Coinmarketcap的API一起工作,那么将他们的nodeJs代码转换为PHP可能是徒劳的。

我假设您在更新发生之前就已经使用cURL来访问其API,并且需要API_KEY来进行请求。

只需将以下内容添加到PHP中的cURL请求中,并将import React from "react"; import Nav from "../../../components/navigation"; import Dropout from "../../../components/dropout"; import CategoryGrid from "../../../components/grid"; import classnames from "classnames"; import style from "./style.scss"; var data = [ { categorytitle: "Shoes", category: [ { Category1: "Boots" }, { Category2: "Sneakers" }, { Category3: "Flats" }, { Category4: "Booties" }, { Category5: "Mules" }, { Category6: "Heels/Pumps" }, { Category7: "Clogs" }, { Category8: "Slippers" }, { Category9: "Sandals" }, { Category10: "Sale" }, { Category11: "Shop All" } ] }, { categorytitle: "Activities", category: [ { Category1: "Comfort Shop" }, { Category2: "Run Shop" }, { Category3: "Trend Guide" }, { Category4: "Athletic" }, { Category5: "Casual" }, { Category6: "Dress" }, { Category7: "Outdoor" }, { Category8: "Walking" }, { Category9: "Foot Health & Wellness" }, { Category10: "Narrow Shoes" }, { Category11: "Wide Shoes" } ] }, { categorytitle: "Clothing & More", category: [ { Category1: "Handbags/Purses" }, { Category2: "Active & Yoga" }, { Category3: "Coats & Jackets" }, { Category4: "Athletic" }, { Category5: "Dresses" }, { Category6: "Tops" }, { Category7: "Sweaters" }, { Category8: "Socks" }, { Category9: "Sunglasses" }, { Category10: "Rain Gear" }, { Category11: "Wide Shoes" } ] }, { categorytitle: "Top Brands", category: [ { Category1: "Sam Edelman" }, { Category2: "Clarks" }, { Category3: "Dr Martens" }, { Category4: "Lucky Brand" }, { Category5: "New Balance" }, { Category6: "Skechers" }, { Category7: "Sperry Top-Sider" }, { Category8: "Sorel" }, { Category9: "TOMS" }, { Category10: "UGG" }, { Category11: "Vionic" } ] }, { targetUrl: "/", imagePath: "/images/image_1.png", title: "WORK SHOP" } ]; let CategoryMenuDropout = false; class Navigation extends React.Component { constructor(props) { super(props); this.state = { currentHoverIndex: -1 }; this.titleHover = this.titleHover.bind(this); this.openCategoryMenuDropout = this.openCategoryMenuDropout.bind(this); this.closeCategoryMenuDropout = this.closeCategoryMenuDropout.bind(this); } titleHover(currentHoverIndex) { this.setState({ showCategoryMenuDropout: true }); } openCategoryMenuDropout() { this.setState({ showCategoryMenuDropout: true }); } closeCategoryMenuDropout() { this.setState({ showCategoryMenuDropout: false }); } render() { return ( <div> <Nav data={this.props.data.map(({ title }, a) => ({ title, target: "" }))} onHover={this.titleHover} /> <Dropout open={this.state.showCategoryMenuDropout} onMouseOut={this.closeCategoryMenuDropout} onMouseEnter={this.openCategoryMenuDropout}> <div className={classnames(style.cartegory_wrapper)} onMouseOut={this.closeCategoryMenuDropout} onMouseEnter={this.openCategoryMenuDropout}> <CategoryGrid> {data.map((ele, index) => ( <div className={classnames(style.cartegory_menu)} key={"Key-" + index}> <h3 className={classnames(style.cartegory_menu_title)}> {ele.categorytitle} </h3> <ul> {Array.isArray(ele.category) && ele.category.map((d, i) => ( <li key={"Key-" + i}>{d[`Category${i + 1}`]}</li> ))} {ele.imagePath && <img src={ele.imagePath} />} </ul> </div> ))} </CategoryGrid> </div> </Dropout> </div> ); } } export default Navigation; 替换为从HERE收到的api密钥:

API_SECRET