影视娱乐类API挪用的代码示例合集:NBA赛事、电视节目等

时间:2022-05-27 04:05:09

**API Shop(apishop.net)供给多达50款的常用第三方API,可以从github上下载代码示例合集:https://github.com/apishop/All-APIs**

以上接口均包罗PHP、Python、C#和Java等四种语言的代码示例,以 获取电视台分类 API为例:

(1)基于PHP的 获取电视台分类 API处事请求的代码示例

<?php $method = "POST"; $url = "https://api.apishop.net/common/tv/queryCategory"; $headers = NULL; $params = array( ); $result = apishop_curl($method, $url, $headers, $params); If ($result) { $body = json_decode($result["body"], TRUE); $status_code = $body["statusCode"]; If ($status_code == "000000") { //状态码为000000, 说明请求告成 echo "请求告成:" . $result["body"]; } else { //状态码非000000, 说明请求掉败 echo "请求掉败:" . $result["body"]; } } else { //返回内容异常,发送请求掉败,以下可按照业务逻辑自行改削 echo "发送请求掉败"; } /** * 转发请求到目的主机 * @param $method string 请求要领 * @param $URL string 请求地点 * @param null $headers 请求头 * @param null $param 请求参数 * @return array|bool */ function apishop_curl(&$method, &$URL, &$headers = NULL, &$param = NULL) { // 初始化请求 $require = curl_init($URL); // 判断是否HTTPS $isHttps = substr($URL, 0, 8) == "https://" ? TRUE : FALSE; // 设置请求方法 switch ($method) { case "GET": curl_setopt($require, CURLOPT_CUSTOMREQUEST, "GET"); break; case "POST": curl_setopt($require, CURLOPT_CUSTOMREQUEST, "POST"); break; default: return FALSE; } if ($param) { curl_setopt($require, CURLOPT_POSTFIELDS, $param); } if ($isHttps) { // 跳过证书查抄 curl_setopt($require, CURLOPT_SSL_VERIFYPEER, FALSE); // 查抄证书中是否设置域名 curl_setopt($require, CURLOPT_SSL_VERIFYHOST, 2); } if ($headers) { // 设置请求头 curl_setopt($require, CURLOPT_HTTPHEADER, $headers); } // 返回功效不直接输出 curl_setopt($require, CURLOPT_RETURNTRANSFER, TRUE); // 重定向 curl_setopt($require, CURLOPT_FOLLOWLOCATION, TRUE); // 把返回头包罗再输出中 curl_setopt($require, CURLOPT_HEADER, TRUE); // 发送请求 $response = curl_exec($require); // 获取头部长度 $headerSize = curl_getinfo($require, CURLINFO_HEADER_SIZE); // *请求 curl_close($require); if ($response) { // 返回头部字符串 $header = substr($response, 0, $headerSize); // 返回体 $body = substr($response, $headerSize); // 过滤隐藏犯警字符 $bodyTemp = json_encode(array( 0 => $body )); $bodyTemp = str_replace("\ufeff", "", $bodyTemp); $bodyTemp = json_decode($bodyTemp, TRUE); $body = trim($bodyTemp[0]); // 将返回功效头部转成数组 $respondHeaders = array(); $header_rows = array_filter(explode(PHP_EOL, $header), "trim"); foreach ($header_rows as $row) { $keylen = strpos($row, ":"); if ($keylen) { $respondHeaders[] = array( "key" => substr($row, 0, $keylen), "value" => trim(substr($row, $keylen + 1)) ); } } return array( "headers" => $respondHeaders, "body" => $body ); } else { return FALSE; } }

(2)基于Python的 获取电视台分类 API处事请求的代码示例

#!/usr/bin/env python # -*- coding: utf-8 -*- # 测试环境: python2.7 # 安置requests依赖 => pip install requests/ easy_install requests # 导入requests依赖 import requests import json import sys reload(sys) sys.setdefaultencoding(‘utf-8‘) def apishop_send_request(method, url, params=None, headers=None): ‘‘‘ 转发请求到目的主机 @param method str 请求要领 @param url str 请求地点 @param params dict 请求参数 @param headers dict 请求头 ‘‘‘ method = str.upper(method) if method == ‘POST‘: return requests.post(url=url, data=params, headers=headers) elif method == ‘GET‘: return requests.get(url=url, params=params, headers=headers) else: return None method = "POST" url = "https://api.apishop.net/common/tv/queryCategory" headers = None params = { } result = apishop_send_request(method=method, url=url, params=params, headers=headers) if result: body = result.text response = json.loads(body) status_code = response["statusCode"] if (status_code == ‘000000‘): # 状态码为000000, 说明请求告成 print(‘请求告成:%s‘ % (body,)) else: # 状态码非000000, 说明请求掉败 print(‘请求掉败: %s‘ % (body,)) else: # 返回内容异常,发送请求掉败 print(‘发送请求掉败‘)

(3)基于C#的 获取电视台分类 API处事请求的代码示例

using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serialization; namespace apishop_sdk { class Program { /** * 转发请求到目的主机 * @param method string 请求要领 * @param url string 请求地点 * @param params Dictionary<string,string> 请求参数 * @param headers Dictionary<string,string> 请求头 * @return string **/ static string apishop_send_request(string method, string url, Dictionary<string, string> param, Dictionary<string, string> headers) { string result = string.Empty; try { string paramData = ""; if (param != null && param.Count > 0) { StringBuilder sbuilder = new StringBuilder(); foreach (var item in param) { if (sbuilder.Length > 0) { sbuilder.Append("&"); } sbuilder.Append(item.Key + "=" + item.Value); } paramData = sbuilder.ToString(); } method = method.ToUpper(); if (method == "GET") { url = string.Format("{0}?{1}", url, paramData); } HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url); if (method == "GET") { wbRequest.Method = "GET"; } else if (method == "POST") { wbRequest.Method = "POST"; wbRequest.ContentType = "application/x-www-form-urlencoded"; wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData); using (Stream requestStream = wbRequest.GetRequestStream()) { using (StreamWriter swrite = new StreamWriter(requestStream)) { swrite.Write(paramData); } } } HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse(); using (Stream responseStream = wbResponse.GetResponseStream()) { using (StreamReader sread = new StreamReader(responseStream)) { result = sread.ReadToEnd(); } } } catch { return ""; } return result; } class Response { public string statusCode; } static void Main(string[] args) { string method = "POST"; string url = "https://api.apishop.net/common/tv/queryCategory"; Dictionary<string, string> param = new Dictionary<string, string>(); Dictionary<string, string> headers = null; string result = apishop_send_request(method, url, param, headers); if (result == "") { //返回内容异常,发送请求掉败 Console.WriteLine("发送请求掉败"); return; } Response res = new JavaScriptSerializer().Deserialize<Response>(result); if (res.statusCode == "000000") { //状态码为000000, 说明请求告成 Console.WriteLine(string.Format("请求告成: {0}", result)); } else { //状态码非000000, 说明请求掉败 Console.WriteLine(string.Format("请求掉败: {0}", result)); } Console.ReadLine(); } } }