PHP調用三方平台API的方法


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.請求結果處理

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM