不同语言调用开放平台接口方法demo

本平台接口现已支持TLS1.3协议,建议优先使用此协议进行访问,可大大提高访问效率,不建议使用TSL1.0协议

PHP代码可参考:

function http_curl($url,$type = 'post',$data = array(),$addHeader = array(),$timeout = 15,$https = false,$headers = false,$usecookie = false) {
 $results['error']   = '';
 $results['status']  = 0;
 $results['data']    = array();
 $user_agent         =  $_SERVER['HTTP_USER_AGENT'];
 $curl               = curl_init();                              // 启动一个CURL会话
 if($type == 'post')
 {
     curl_setopt($curl, CURLOPT_POST, 1);                        // 发送一个常规的Post请求
     if (!empty($data) && is_array($data)) {
     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);              // Post提交的数据包
     }
 }
 if($https){
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);              // 对认证证书来源的检查
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);              // 从证书中检查SSL加密算法是否存在
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);              // 使用自动跳转
     curl_setopt($curl, CURLOPT_CAINFO,'/data/wwwroot/ecdaohang_applation/cacert.pem');
   }
     curl_setopt($curl, CURLOPT_URL, $url);                      // 要访问的地址
     curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);                    // 设置超时限制防止死循环
     if (!empty($addHeader) && is_array($addHeader)) {
     curl_setopt($curl, CURLOPT_HTTPHEADER, $addHeader);
     }
                 if ($headers) {
                         curl_setopt($curl, CURLOPT_HEADER, 1);
                }else {
                        curl_setopt($curl, CURLOPT_HEADER, 0);
                }
                if ($usecookie) {
                        curl_setopt($curl, CURLOPT_COOKIEJAR,  '/data/wwwroot/th_apps/tk_apps/runtime/a_cookie.txt');
                        curl_setopt($curl, CURLOPT_COOKIEFILE, '/data/wwwroot/th_apps/tk_apps/runtime/a_cookie.txt');
                }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);              // 获取的信息以文件流的形式返回
     curl_setopt($curl, CURLOPT_USERAGENT,$user_agent);          // 模拟用户使用的浏览器
     curl_setopt($curl, CURLOPT_AUTOREFERER, 1);                 // 自动设置Referer

 $results['data'] = curl_exec($curl);                     // 执行操作
 if (curl_errno($curl))
 {
    $results['error']    = curl_error($curl);                    //捕抓异常
 }
 curl_close($curl);                                              // 关闭CURL会话
 return $results;
}

调用方法:

http_curl('https://api.open.21ds.cn/apiv1/getitemgyurl?apkey=xxxxxx&itemid=529048526383&pid=mm_0000_0000_000&tbname=xxx','get')

Python代码可参考:

import requests
import json
openurl = ('https://api.open.21ds.cn/apiv1/gettkmaterial?apkey=%s&adzoneid=%s&siteid=%s&tbname=%s&pageno=%s&cat=%s&hascoupon=true' % (config.apkey,config.adzoneid,config.siteid,config.tbname,page,cate))
resp= requests.get(openurl)
content = resp.content
content = json.loads(content)
print(content)

JAVA代码可参考:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class HttpURLConnectionExample {

    private final String USER_AGENT = "Mozilla/5.0";

    public static void main(String[] args) throws Exception {

        HttpURLConnectionExample http = new HttpURLConnectionExample();

        System.out.println("Testing 1 - Send Http GET request");
        http.sendGet();

        System.out.println("\nTesting 2 - Send Http POST request");
        http.sendPost();

    }

    // HTTP GET请求
    private void sendGet() throws Exception {

        String url = "https://api.open.21ds.cn/apiv1/getitemgyurl?apkey=xxxxxx&itemid=529048526383&pid=mm_0000_0000_000&tbname=xxx";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //默认值我GET
        con.setRequestMethod("GET");

        //添加请求头
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //打印结果
        System.out.println(response.toString());

    }
}

.NET程序需加协议说明:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
//.net 4.5以上可直接用
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注