1.说明
在后台开发过程中,经常会用的调用第三方平台api的情况来获取一些信息或者资讯作为数据和功能的补充。下面笔者就以极速数据平台的新闻API为例、PHP为后台语言做请示示例。
2.具体操作
1.准备工作
对于接口调用,必然会用到PHP的cURL 函数,函数不是特别复杂,具体用法及其参数可以参考手册。极速数据平台封装了一个非常好用的请求函数,链接为:https://www.jisuapi.com/code/694,笔者已将代码搬运过来:
<?php /** * 使用: * echo curlOpen(\'https://www.baidu.com\'); * * POST数据 * $post = array(\'aa\'=>\'ddd\',\'ee\'=>\'d\') * 或 * $post = \'aa=ddd&ee=d\'; * echo curlOpen(\'https://www.baidu.com\',array(\'post\'=>$post)); * @param string $url * @param array $config */ function curlOpen($url, $config = array()) { $arr = array(\'post\' => false,\'referer\' => $url,\'cookie\' => \'\', \'useragent\' => \'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)\', \'timeout\' => 20, \'return\' => true, \'proxy\' => \'\', \'userpwd\' => \'\', \'nobody\' => false,\'header\'=>array(),\'gzip\'=>true,\'ssl\'=>false,\'isupfile\'=>false); $arr = array_merge($arr, $config); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr[\'return\']); curl_setopt($ch, CURLOPT_NOBODY, $arr[\'nobody\']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_USERAGENT, $arr[\'useragent\']); curl_setopt($ch, CURLOPT_REFERER, $arr[\'referer\']); curl_setopt($ch, CURLOPT_TIMEOUT, $arr[\'timeout\']); //curl_setopt($ch, CURLOPT_HEADER, true);//获取header if($arr[\'gzip\']) curl_setopt($ch, CURLOPT_ENCODING, \'gzip,deflate\'); if($arr[\'ssl\']) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if(!empty($arr[\'cookie\'])) { curl_setopt($ch, CURLOPT_COOKIEJAR, $arr[\'cookie\']); curl_setopt($ch, CURLOPT_COOKIEFILE, $arr[\'cookie\']); } if(!empty($arr[\'proxy\'])) { //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); curl_setopt ($ch, CURLOPT_PROXY, $arr[\'proxy\']); if(!empty($arr[\'userpwd\'])) { curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr[\'userpwd\']); } } //ip比较特殊,用键值表示 if(!empty($arr[\'header\'][\'ip\'])) { array_push($arr[\'header\'],\'X-FORWARDED-FOR:\'.$arr[\'header\'][\'ip\'],\'CLIENT-IP:\'.$arr[\'header\'][\'ip\']); unset($arr[\'header\'][\'ip\']); } $arr[\'header\'] = array_filter($arr[\'header\']); if(!empty($arr[\'header\'])) { curl_setopt($ch, CURLOPT_HTTPHEADER, $arr[\'header\']); } if ($arr[\'post\'] != false) { curl_setopt($ch, CURLOPT_POST, true); if(is_array($arr[\'post\']) && $arr[\'isupfile\'] === false) { $post = http_build_query($arr[\'post\']); } else { $post = $arr[\'post\']; } curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } $result = curl_exec($ch); //var_dump(curl_getinfo($ch)); curl_close($ch); return $result; }
然后需要在极速数据平台申请账号(https://www.jisuapi.com),并申请数据。(新闻接口为免费接口,每天有100次的免费次数使用)。
2.接口调用
先看请求方式
GET,POST都支持
在看请求的参数:
| 参数名称 | 类型 | 必填 | 说明 |
| channel | string | 是 | 频道 |
| num | int | 否 | 默认10 |
| start | int | 否 | 0 |
调用方式:
<?php require_once \'curl.func.php\'; $appkey = \'your_appkey_here\';//你的appkey $channel=\'头条\';//utf8 新闻频道(头条,财经,体育,娱乐,军事,教育,科技,NBA,股票,星座,女性,健康,育儿) $url = "https://api.jisuapi.com/news/get?channel=$channel&appkey=$appkey"; $result = curlOpen($url, [\'ssl\'=>true]); $jsonarr = json_decode($result, true); if($jsonarr[\'status\'] != 0) { echo $jsonarr[\'msg\']; exit(); } $result = $jsonarr[\'result\']; echo $result[\'channel\'].\' \'.$result[\'num\']. \'<br>\'; foreach($result[\'list\'] as $val) { echo $val[\'title\'].\' \'.$val[\'time\'].\' \'.$val[\'src\'].\' \'.$val[\'category\'].\' \'.$val[\'pic\'].\' \'.$val[\'content\'].\' \'.$val[\'url\'].\' \'.$val[\'weburl\'] . \'<br>\'; }
3.结果显示
{ "status": 0, "msg": "ok", "result": { "channel": "头条", "num": 10, "list": [ { "title": "任正非\"大胆\"提议:向西方出售5G技术 主动制造对手", "time": "2019-09-12 17:08:23", "src": "观察者网", "category": "news", "pic": "https://cms-bucket.ws.126.net/2019/09/12/a5ebae57f5be40ca9c407fd89cf0da18.png", "content": "<div class="\"content\"">\n<div class="\"page" js-page="" on\"="">\n<p>作为下一代的极速移动通信网络,5G将很快把从汽车到工业机器人的一切东西连接起来..."
"url": "http://3g.163.com/news/19/0912/17/EOT02I0V0001899O.html", "weburl": "http://news.163.com/19/0912/17/EOT02I0V0001899O.html" } }
然后再将json转化成数组,在对数字进行后续操作
3.总结
总的来说调用接口并不是一件很难的事,但是几个点需要注意
1.调用的方式,POST和GET在cURL里面的请求参数略微有点区别
2.请求的参数,必填的参数必须提交过去
3.请求结果处理