如何编写基于PHP的足球联赛查询接口调用示例代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计737个文字,预计阅读时间需要3分钟。
基于PHP的足球联赛查询接口调用代码示例:php
// 初始化接口参数$lid=1; // 联赛ID$num=20; // 返回数量$dtype='json'; // 返回格式
// 初始化请求参数$api_key='你的API密钥'; // 替换为你的API密钥$url=http://v.juhe.cn/football/league.php?lid={$lid}&num={$num}&dtype={$dtype}&key={$api_key};
// 发起HTTP请求$response=file_get_contents($url);
// 解析返回结果$result=json_decode($response, true);
// 输出结果if ($result['error_code']==0) { echo 请求成功; foreach ($result['result']['data'] as $match) { echo 赛事ID:{$match['match_id']}\n; echo 比赛时间:{$match['match_time']}\n; echo 主队:{$match['home_team']}\n; echo 客队:{$match['guest_team']}\n; echo 比分:{$match['score']}\n; echo ---------------------------------\n; }} else { echo 请求失败,错误码:{$result['error_code']},错误信息:{$result['reason']};}?>
基于php的足球联赛查询接口调用代码实例<!--?php // +---------------------------------------------------------------------- // | JuhePHP [ NO ZUO NO DIE ] // +---------------------------------------------------------------------- // | Copyright (c) 2010-2015 juhe.cn All rights reserved. // +---------------------------------------------------------------------- // | Author: Juhedata <info@juhe.cn--> // +---------------------------------------------------------------------- //---------------------------------- // 足球联赛调用示例代码 - 聚合数据 // 在线接口文档:www.juhe.cn/docs/90 //---------------------------------- header('Content-type:text/html;charset=utf-8'); //配置您申请的appkey $appkey = "*********************"; //************1.足球联赛赛事查询************ $url = "op.juhe.cn/onebox/football/league"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json "league" => "",//联赛名称 ); $paramstring = op.juhe.cn/onebox/football/team"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json "team" => "",//球队名称 ); $paramstring = op.juhe.cn/onebox/football/combat"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json "hteam" => "",//主队球队名称 "vteam" => "",//客队球队名称 ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "请求失败"; } //************************************************** /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ function juhecurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { //echo "cURL Error: " . curl_error($ch); return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; }
本文共计737个文字,预计阅读时间需要3分钟。
基于PHP的足球联赛查询接口调用代码示例:php
// 初始化接口参数$lid=1; // 联赛ID$num=20; // 返回数量$dtype='json'; // 返回格式
// 初始化请求参数$api_key='你的API密钥'; // 替换为你的API密钥$url=http://v.juhe.cn/football/league.php?lid={$lid}&num={$num}&dtype={$dtype}&key={$api_key};
// 发起HTTP请求$response=file_get_contents($url);
// 解析返回结果$result=json_decode($response, true);
// 输出结果if ($result['error_code']==0) { echo 请求成功; foreach ($result['result']['data'] as $match) { echo 赛事ID:{$match['match_id']}\n; echo 比赛时间:{$match['match_time']}\n; echo 主队:{$match['home_team']}\n; echo 客队:{$match['guest_team']}\n; echo 比分:{$match['score']}\n; echo ---------------------------------\n; }} else { echo 请求失败,错误码:{$result['error_code']},错误信息:{$result['reason']};}?>
基于php的足球联赛查询接口调用代码实例<!--?php // +---------------------------------------------------------------------- // | JuhePHP [ NO ZUO NO DIE ] // +---------------------------------------------------------------------- // | Copyright (c) 2010-2015 juhe.cn All rights reserved. // +---------------------------------------------------------------------- // | Author: Juhedata <info@juhe.cn--> // +---------------------------------------------------------------------- //---------------------------------- // 足球联赛调用示例代码 - 聚合数据 // 在线接口文档:www.juhe.cn/docs/90 //---------------------------------- header('Content-type:text/html;charset=utf-8'); //配置您申请的appkey $appkey = "*********************"; //************1.足球联赛赛事查询************ $url = "op.juhe.cn/onebox/football/league"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json "league" => "",//联赛名称 ); $paramstring = op.juhe.cn/onebox/football/team"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json "team" => "",//球队名称 ); $paramstring = op.juhe.cn/onebox/football/combat"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "dtype" => "",//返回数据的格式,xml或json,默认json "hteam" => "",//主队球队名称 "vteam" => "",//客队球队名称 ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "请求失败"; } //************************************************** /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ function juhecurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { //echo "cURL Error: " . curl_error($ch); return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; }

