全国省市今日油价
请求Header:
名称 | 值 | |
---|---|---|
Content-Type | application/x-www-form-urlencoded |
请求参数说明:
名称 | 必填 | 类型 | 说明 | |
---|---|---|---|---|
key | 是 | string | 在个人中心->我的数据,接口名称上方查看 | |
dtype | 否 | string | 返回数据的格式,xml或json,默认json |
请求代码示例:
curl -k -i "http://apis.juhe.cn/cnoil/oil_city?key=key&dtype=json"
我的数据,接口名称上方查看
// 接口请求入参配置
$requestParams = [
'key' => $apiKey,
'dtype'=> 'json',
];
$requestParamsStr = http_build_query($requestParams);
// 发起接口网络请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $apiUrl . '?' . $requestParamsStr);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (1 == strpos("$" . $apiUrl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$response = curl_exec($curl);
$httpInfo = curl_getinfo($curl);
curl_close($curl);
// 解析响应结果
$responseResult = json_decode($response, true);
if ($responseResult) {
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
var_dump($responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
// var_dump($httpInfo);
var_dump("请求异常");
}
import requests
# 110-全国省市今日油价 - 代码参考(根据实际业务情况修改)
# 基本参数配置
apiUrl = 'http://apis.juhe.cn/cnoil/oil_city' # 接口请求URL
apiKey = '您申请的调用APIkey' # 在个人中心->我的数据,接口名称上方查看
# 接口请求入参配置
requestParams = {
'key': apiKey,
'dtype': 'json',
}
# 发起接口网络请求
response = requests.get(apiUrl, params=requestParams)
# 解析响应结果
if response.status_code == 200:
responseResult = response.json()
# 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
print(responseResult)
else:
# 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
print('请求异常')
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func main() {
// 基本参数配置
apiUrl := "http://apis.juhe.cn/cnoil/oil_city"
apiKey := "您申请的调用APIkey"
// 接口请求入参配置
requestParams := url.Values{}
requestParams.Set("key", apiKey)
requestParams.Set("dtype", "json")
// 发起接口网络请求
resp, err := http.Get(apiUrl + "?" + requestParams.Encode())
if err != nil {
fmt.Println("网络请求异常:", err)
return
}
defer resp.Body.Close()
var responseResult map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&responseResult)
if err != nil {
fmt.Println("解析响应结果异常:", err)
return
}
fmt.Println(responseResult)
}
using System;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
namespace Common_API_Test.Test_Demo
{
class Csharp_get
{
static void Main(string[] args)
{
string url = "http://apis.juhe.cn/cnoil/oil_city";
string apiKey = "您申请的调用APIkey";
Dictionary data = new Dictionary();
data.Add("key", apiKey);
data.Add( "dtype", "json");
using (WebClient client = new WebClient())
{
string fullUrl = url + "?" + string.Join("&", data.Select(x => x.Key + "=" + x.Value));
try
{
string responseContent = client.DownloadString(fullUrl);
dynamic responseData = JsonConvert.DeserializeObject(responseContent);
if (responseData != null)
{
Console.WriteLine("Return Code: " + responseData["error_code"]);
Console.WriteLine("Return Message: " + responseData["reason"]);
}
else
{
Console.WriteLine("json解析异常!");
}
}
catch (Exception)
{
Console.WriteLine("请检查其它错误");
}
}
}
}
}
const axios = require('axios'); // npm install axios
// 基本参数配置
const apiUrl = 'http://apis.juhe.cn/cnoil/oil_city'; // 接口请求URL
const apiKey = '您申请的调用APIkey'; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
const requestParams = {
key: apiKey,
dtype: 'json',
};
// 发起接口网络请求
axios.get(apiUrl, {params: requestParams})
.then(response => {
// 解析响应结果
if (response.status === 200) {
const responseResult = response.data;
// 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
console.log(responseResult);
} else {
// 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
console.log('请求异常');
}
})
.catch(error => {
// 网络请求失败,可以根据实际情况进行处理
console.log('网络请求失败:', error);
});
package cn.juhe.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaGet {
public static void main(String[] args) throws Exception {
String apiKey = "你申请的key";
String apiUrl = "http://apis.juhe.cn/cnoil/oil_city";
HashMap map = new HashMap<>();
map.put("key", apiKey);
map.put("dtype", "json");
URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}
public static String params(Map map) {
return map.entrySet().stream()
.map(entry -> {
try {
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
} catch (Exception e) {
e.printStackTrace();
return entry.getKey() + "=" + entry.getValue();
}
})
.collect(Collectors.joining("&"));
}
}
// 基本参数配置
NSString *apiUrl = @"http://apis.juhe.cn/cnoil/oil_city"; // 接口请求URL
NSString *apiKey = @"您申请的调用APIkey"; // 在个人中心->我的数据,接口名称上方查看
// 接口请求入参配置
NSDictionary *requestParams = @{
@"key": apiKey,
@"dtype": @"json",
};
// 发起接口网络请求
NSURLComponents *components = [NSURLComponents componentsWithString:apiUrl];
NSMutableArray *queryItems = [NSMutableArray array];
[requestParams enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[queryItems addObject:[NSURLQueryItem queryItemWithName:key value:value]];
}];
components.queryItems = queryItems;
NSURL *url = components.URL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// 网络请求异常处理
NSLog(@"请求异常");
} else {
NSError *jsonError;
NSDictionary *responseResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (!jsonError) {
// 网络请求成功处理
// NSLog(@"%@", [responseResult objectForKey:@"error_code"]);
NSLog(@"%@", responseResult);
} else {
// 解析结果异常处理
NSLog(@"解析结果异常");
}
}
}];
[task resume];
返回参数说明:
名称 | 类型 | 说明 | |
---|---|---|---|
error_code | int | 返回码 | |
reason | string | 返回说明 | |
result | string | 返回结果集 |
JSON返回示例:JSON在线格式化工具 >
{
"resultcode": "200",
"reason": "查询成功!",
"result": [
{
"city": "北京",
"b90": "-",/*89(90)号*/
"b93": "7.99",/*92(93)号*/
"b97": "8.50",/*95(97)号*/
"b0": "7.47",/*0号*/
"92h": "7.99",/*建议取值*/
"95h": "8.50",/*建议取值*/
"98h": "7.47",/*建议取值*/
"0h": "7.47"/*建议取值*/
},
{
"city": "天津",
"b90": "-",
"b93": "7.98",
"b97": "8.43",
"b0": "7.40",
"92h": "7.98",
"95h": "8.43",
"98h": "7.40",
"0h": "7.40"
},
{
"city": "河北",
"b90": "-",
"b93": "7.98",
"b97": "8.43",
"b0": "7.40",
"92h": "7.98",
"95h": "8.43",
"98h": "7.40",
"0h": "7.40"
},
{
"city": "山西",
"b90": "-",
"b93": "7.93",
"b97": "8.56",
"b0": "5.64",
"92h": "7.93",
"95h": "8.56",
"98h": "5.64",
"0h": "5.64"
},
{
"city": "内蒙古",
"b90": "-",
"b93": "7.94",
"b97": "8.52",
"b0": "0.00",
"92h": "7.94",
"95h": "8.52",
"98h": "0.00",
"0h": "0.00"
},
{
"city": "辽宁",
"b90": "-",
"b93": "7.96",
"b97": "8.49",
"b0": "5.86",
"92h": "7.96",
"95h": "8.49",
"98h": "5.86",
"0h": "5.86"
},
{
"city": "吉林",
"b90": "-",
"b93": "7.82",
"b97": "8.43",
"b0": "6.10",
"92h": "7.82",
"95h": "8.43",
"98h": "6.10",
"0h": "6.10"
},
{
"city": "黑龙江",
"b90": "-",
"b93": "7.84",
"b97": "8.36",
"b0": "0.00",
"92h": "7.84",
"95h": "8.36",
"98h": "0.00",
"0h": "0.00"
},
{
"city": "上海",
"b90": "-",
"b93": "7.95",
"b97": "8.46",
"b0": "6.62",
"92h": "7.95",
"95h": "8.46",
"98h": "6.62",
"0h": "6.62"
},
{
"city": "江苏",
"b90": "-",
"b93": "7.95",
"b97": "8.46",
"b0": "6.66",
"92h": "7.95",
"95h": "8.46",
"98h": "6.66",
"0h": "6.66"
},
{
"city": "浙江",
"b90": "-",
"b93": "7.49",
"b97": "8.32",
"b0": "5.99",
"92h": "7.49",
"95h": "8.32",
"98h": "5.99",
"0h": "5.99"
},
{
"city": "安徽",
"b90": "-",
"b93": "7.93",
"b97": "8.49",
"b0": "7.44",
"92h": "7.93",
"95h": "8.49",
"98h": "7.44",
"0h": "7.44"
},
{
"city": "福建",
"b90": "-",
"b93": "7.95",
"b97": "8.48",
"b0": "5.62",
"92h": "7.95",
"95h": "8.48",
"98h": "5.62",
"0h": "5.62"
},
{
"city": "江西",
"b90": "-",
"b93": "7.94",
"b97": "8.53",
"b0": "7.38",
"92h": "7.94",
"95h": "8.53",
"98h": "7.38",
"0h": "7.38"
},
{
"city": "山东",
"b90": "-",
"b93": "7.97",
"b97": "8.55",
"b0": "5.54",
"92h": "7.97",
"95h": "8.55",
"98h": "5.54",
"0h": "5.54"
},
{
"city": "河南",
"b90": "-",
"b93": "7.99",
"b97": "8.54",
"b0": "5.68",
"92h": "7.99",
"95h": "8.54",
"98h": "5.68",
"0h": "5.68"
},
{
"city": "湖北",
"b90": "-",
"b93": "7.86",
"b97": "8.42",
"b0": "5.40",
"92h": "7.86",
"95h": "8.42",
"98h": "5.40",
"0h": "5.40"
},
{
"city": "湖南",
"b90": "-",
"b93": "7.80",
"b97": "8.29",
"b0": "6.64",
"92h": "7.80",
"95h": "8.29",
"98h": "6.64",
"0h": "6.64"
},
{
"city": "广东",
"b90": "-",
"b93": "8.00",
"b97": "8.67",
"b0": "7.43",
"92h": "8.00",
"95h": "8.67",
"98h": "7.43",
"0h": "7.43"
},
{
"city": "广西",
"b90": "-",
"b93": "8.04",
"b97": "8.69",
"b0": "7.49",
"92h": "8.04",
"95h": "8.69",
"98h": "7.49",
"0h": "7.49"
},
{
"city": "海南",
"b90": "-",
"b93": "9.10",
"b97": "9.67",
"b0": "7.61",
"92h": "9.10",
"95h": "9.67",
"98h": "7.61",
"0h": "7.61"
},
{
"city": "重庆",
"b90": "-",
"b93": "8.05",
"b97": "8.50",
"b0": "7.61",
"92h": "8.05",
"95h": "8.50",
"98h": "7.61",
"0h": "7.61"
},
{
"city": "四川",
"b90": "-",
"b93": "8.00",
"b97": "8.62",
"b0": "6.12",
"92h": "8.00",
"95h": "8.62",
"98h": "6.12",
"0h": "6.12"
},
{
"city": "贵州",
"b90": "-",
"b93": "8.11",
"b97": "8.57",
"b0": "6.94",
"92h": "8.11",
"95h": "8.57",
"98h": "6.94",
"0h": "6.94"
},
{
"city": "云南",
"b90": "-",
"b93": "8.13",
"b97": "8.73",
"b0": "7.49",
"92h": "8.13",
"95h": "8.73",
"98h": "7.49",
"0h": "7.49"
},
{
"city": "西藏",
"b90": "-",
"b93": "8.86",
"b97": "9.37",
"b0": "7.23",
"92h": "8.86",
"95h": "9.37",
"98h": "7.23",
"0h": "7.23"
},
{
"city": "陕西",
"b90": "-",
"b93": "7.87",
"b97": "8.31",
"b0": "5.67",
"92h": "7.87",
"95h": "8.31",
"98h": "5.67",
"0h": "5.67"
},
{
"city": "甘肃",
"b90": "-",
"b93": "7.87",
"b97": "8.41",
"b0": "5.51",
"92h": "7.87",
"95h": "8.41",
"98h": "5.51",
"0h": "5.51"
},
{
"city": "宁夏",
"b90": "-",
"b93": "7.88",
"b97": "8.33",
"b0": "5.56",
"92h": "7.88",
"95h": "8.33",
"98h": "5.56",
"0h": "5.56"
},
{
"city": "新疆",
"b90": "-",
"b93": "7.90",
"b97": "8.51",
"b0": "6.06",
"92h": "7.90",
"95h": "8.51",
"98h": "6.06",
"0h": "6.06"
}
],
"error_code": 0
}
服务级错误码参照(error_code):
错误码 | 说明 | |
---|---|---|
204800 | 数据更新中… | |
204801 | 网络错误 | |
204802 | 无数据 | |
204803 | 未知错误 |
系统级错误码参照:
错误码 | 说明 | 旧版本(resultcode) | |
---|---|---|---|
10001 | 错误的请求KEY | 101 | |
10002 | 该KEY无请求权限 | 102 | |
10003 | KEY过期 | 103 | |
10004 | 错误的OPENID | 104 | |
10005 | 应用未审核超时,请提交认证 | 105 | |
10007 | 未知的请求源 | 107 | |
10008 | 被禁止的IP | 108 | |
10009 | 被禁止的KEY | 109 | |
10011 | 当前IP请求超过限制 | 111 | |
10012 | 请求超过次数限制 | 112 | |
10013 | 测试KEY超过请求限制 | 113 | |
10014 | 系统内部异常(调用充值类业务时,请务必联系客服或通过订单查询接口检测订单,避免造成损失) | 114 | |
10020 | 接口维护 | 120 | |
10021 | 接口停用 | 121 |
错误码格式说明(示例:200201):
2 | 002 | 01 | |
---|---|---|---|
服务级错误(1为系统级错误) | 服务模块代码(即数据ID) | 具体错误代码 |