老衲要吃肉 發表於 2019-9-12 18:07:00

PHP调用三方平台API的方法

<h1>1.说明</h1>
<p>&nbsp; &nbsp; &nbsp; 在后台开发过程中,经常会用的调用第三方平台api的情况来获取一些信息或者资讯作为数据和功能的补充。下面笔者就以极速数据平台的新闻API为例、PHP为后台语言做请示示例。</p>
<h1>2.具体操作</h1>
<h2>1.准备工作</h2>
<p>对于接口调用,必然会用到PHP的<span style="background-color: rgba(153, 204, 0, 1)"><span style="background-color: rgba(153, 204, 0, 1)">cURL 函数</span></span>,函数不是特别复杂,具体用法及其参数可以参考手册。极速数据平台封装了一个非常好用的请求函数,链接为:<span style="background-color: rgba(153, 204, 0, 1)">https://www.jisuapi.com/code/694</span>,笔者已将代码搬运过来:</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 使用:
* echo curlOpen('https://www.baidu.com');
*
* POST数据
* $post = array('aa'=&gt;'ddd','ee'=&gt;'d')
* 或
* $post = 'aa=ddd&amp;ee=d';
* echo curlOpen('https://www.baidu.com',array('post'=&gt;$post));
* @param string $url
* @param array $config
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> curlOpen(<span style="color: rgba(128, 0, 128, 1)">$url</span>, <span style="color: rgba(128, 0, 128, 1)">$config</span> = <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">())
{
    </span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = <span style="color: rgba(0, 0, 255, 1)">array</span>('post' =&gt; <span style="color: rgba(0, 0, 255, 1)">false</span>,'referer' =&gt; <span style="color: rgba(128, 0, 128, 1)">$url</span>,'cookie' =&gt; '', 'useragent' =&gt; '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' =&gt; 20, 'return' =&gt; <span style="color: rgba(0, 0, 255, 1)">true</span>, 'proxy' =&gt; '', 'userpwd' =&gt; '', 'nobody' =&gt; <span style="color: rgba(0, 0, 255, 1)">false</span>,'header'=&gt;<span style="color: rgba(0, 0, 255, 1)">array</span>(),'gzip'=&gt;<span style="color: rgba(0, 0, 255, 1)">true</span>,'ssl'=&gt;<span style="color: rgba(0, 0, 255, 1)">false</span>,'isupfile'=&gt;<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(128, 0, 128, 1)">$arr</span> = <span style="color: rgba(0, 128, 128, 1)">array_merge</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>, <span style="color: rgba(128, 0, 128, 1)">$config</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(128, 0, 128, 1)">$ch</span> =<span style="color: rgba(0, 0, 0, 1)"> curl_init();
   
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_URL, <span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['return'<span style="color: rgba(0, 0, 0, 1)">]);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_NOBODY, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['nobody'<span style="color: rgba(0, 0, 0, 1)">]);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_FOLLOWLOCATION, 1<span style="color: rgba(0, 0, 0, 1)">);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_USERAGENT, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['useragent'<span style="color: rgba(0, 0, 0, 1)">]);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_REFERER, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['referer'<span style="color: rgba(0, 0, 0, 1)">]);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_TIMEOUT, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['timeout'<span style="color: rgba(0, 0, 0, 1)">]);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">curl_setopt($ch, CURLOPT_HEADER, true);//获取header</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['gzip']) curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_ENCODING, 'gzip,deflate'<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['ssl'<span style="color: rgba(0, 0, 0, 1)">])
    {
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['cookie'<span style="color: rgba(0, 0, 0, 1)">]))
    {
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_COOKIEJAR, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['cookie'<span style="color: rgba(0, 0, 0, 1)">]);
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_COOKIEFILE, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['cookie'<span style="color: rgba(0, 0, 0, 1)">]);
    }
   
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['proxy'<span style="color: rgba(0, 0, 0, 1)">]))
    {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); </span>
      curl_setopt (<span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_PROXY, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['proxy'<span style="color: rgba(0, 0, 0, 1)">]);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['userpwd'<span style="color: rgba(0, 0, 0, 1)">]))
      {         
            curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>,CURLOPT_PROXYUSERPWD,<span style="color: rgba(128, 0, 128, 1)">$arr</span>['userpwd'<span style="color: rgba(0, 0, 0, 1)">]);
      }      
    }   
   
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ip比较特殊,用键值表示</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header']['ip'<span style="color: rgba(0, 0, 0, 1)">]))
    {
      </span><span style="color: rgba(0, 128, 128, 1)">array_push</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header'],'X-FORWARDED-FOR:'.<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header']['ip'],'CLIENT-IP:'.<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header']['ip'<span style="color: rgba(0, 0, 0, 1)">]);
      </span><span style="color: rgba(0, 0, 255, 1)">unset</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header']['ip'<span style="color: rgba(0, 0, 0, 1)">]);
    }
    </span><span style="color: rgba(128, 0, 128, 1)">$arr</span>['header'] = <span style="color: rgba(0, 128, 128, 1)">array_filter</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header'<span style="color: rgba(0, 0, 0, 1)">]);
   
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['header'<span style="color: rgba(0, 0, 0, 1)">]))
    {
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_HTTPHEADER, <span style="color: rgba(128, 0, 128, 1)">$arr</span>['header'<span style="color: rgba(0, 0, 0, 1)">]);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$arr</span>['post'] != <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
    {
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_POST, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 128, 128, 1)">is_array</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['post']) &amp;&amp; <span style="color: rgba(128, 0, 128, 1)">$arr</span>['isupfile'] === <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">)
      {
            </span><span style="color: rgba(128, 0, 128, 1)">$post</span> = <span style="color: rgba(0, 128, 128, 1)">http_build_query</span>(<span style="color: rgba(128, 0, 128, 1)">$arr</span>['post'<span style="color: rgba(0, 0, 0, 1)">]);         
      }
      </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
      {
            </span><span style="color: rgba(128, 0, 128, 1)">$post</span> = <span style="color: rgba(128, 0, 128, 1)">$arr</span>['post'<span style="color: rgba(0, 0, 0, 1)">];
      }
      curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: rgba(128, 0, 128, 1)">$post</span><span style="color: rgba(0, 0, 0, 1)">);
    }   
    </span><span style="color: rgba(128, 0, 128, 1)">$result</span> = curl_exec(<span style="color: rgba(128, 0, 128, 1)">$ch</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">var_dump(curl_getinfo($ch));</span>
    curl_close(<span style="color: rgba(128, 0, 128, 1)">$ch</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$result</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>然后需要在极速数据平台申请账号(https://www.jisuapi.com),并申请数据。(新闻接口为免费接口,每天有100次的免费次数使用)。</p>
<h2>2.接口调用</h2>
<p>先看请求方式</p>
<p><span style="background-color: rgba(153, 204, 0, 1)">GET,POST都支持</span></p>
<p>在看请求的参数:</p>
<table style="height: 88px; width: 838px" border="0">
<tbody>
<tr>
<td style="text-align: left">参数名称</td>
<td>类型</td>
<td>必填</td>
<td>说明</td>
</tr>
<tr>
<td>channel</td>
<td>string</td>
<td><span style="background-color: rgba(153, 204, 0, 1)">是</span></td>
<td>频道</td>
</tr>
<tr>
<td>num</td>
<td>int</td>
<td>否</td>
<td>默认10</td>
</tr>
<tr>
<td>start</td>
<td>int</td>
<td>否</td>
<td>0</td>
</tr>
</tbody>
</table>
<p style="text-align: left"><span style="background-color: rgba(153, 204, 0, 1)">调用方式</span>:</p>
<div class="cnblogs_code" style="text-align: left">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php

</span><span style="color: rgba(0, 0, 255, 1)">require_once</span> 'curl.func.php'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(128, 0, 128, 1)">$appkey</span> = 'your_appkey_here';<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">你的appkey</span>
<span style="color: rgba(128, 0, 128, 1)">$channel</span>='头条';<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">utf8 新闻频道(头条,财经,体育,娱乐,军事,教育,科技,NBA,股票,星座,女性,健康,育儿)</span>
<span style="color: rgba(128, 0, 128, 1)">$url</span> = "https://api.jisuapi.com/news/get?channel=<span style="color: rgba(128, 0, 128, 1)">$channel</span>&amp;appkey=<span style="color: rgba(128, 0, 128, 1)">$appkey</span>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$result</span> = curlOpen(<span style="color: rgba(128, 0, 128, 1)">$url</span>, ['ssl'=&gt;<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">]);
</span><span style="color: rgba(128, 0, 128, 1)">$jsonarr</span> = json_decode(<span style="color: rgba(128, 0, 128, 1)">$result</span>, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(128, 0, 128, 1)">$jsonarr</span>['status'] != 0<span style="color: rgba(0, 0, 0, 1)">)
{
    </span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$jsonarr</span>['msg'<span style="color: rgba(0, 0, 0, 1)">];
    </span><span style="color: rgba(0, 0, 255, 1)">exit</span><span style="color: rgba(0, 0, 0, 1)">();
}
</span><span style="color: rgba(128, 0, 128, 1)">$result</span> = <span style="color: rgba(128, 0, 128, 1)">$jsonarr</span>['result'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$result</span>['channel'].' '.<span style="color: rgba(128, 0, 128, 1)">$result</span>['num']. '&lt;br&gt;'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span>(<span style="color: rgba(128, 0, 128, 1)">$result</span>['list'] <span style="color: rgba(0, 0, 255, 1)">as</span> <span style="color: rgba(128, 0, 128, 1)">$val</span><span style="color: rgba(0, 0, 0, 1)">)
{
    </span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 128, 1)">$val</span>['title'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['time'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['src'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['category'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['pic'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['content'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['url'].' '.<span style="color: rgba(128, 0, 128, 1)">$val</span>['weburl'] . '&lt;br&gt;'<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<h2 style="text-align: left">3.结果显示</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
    </span>"status": 0,
    "msg": "ok",
    "result":<span style="color: rgba(0, 0, 0, 1)"> {
      </span>"channel": "头条",
      "num": 10,
      "list":<span style="color: rgba(0, 0, 0, 1)"> [
            {
                </span>"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": "&lt;div class="\"content\""&gt;\n&lt;div <span style="color: rgba(0, 0, 255, 1)">class</span>="\"page" js-page="" on\"=""&gt;\n&lt;p&gt;作为下一代的极速移动通信网络,5G将很快把从汽车到工业机器人的一切东西连接起来..."<br>                "url": "http://3g.163.com/news/19/0912/17/EOT02I0V0001899O.html",
                "weburl": "http://news.163.com/19/0912/17/EOT02I0V0001899O.html"<span style="color: rgba(0, 0, 0, 1)">
            }
}</span></pre>
</div>
<p>然后再将json转化成数组,在对数字进行后续操作</p>
<h1>3.总结</h1>
<p>总的来说调用接口并不是一件很难的事,但是几个点需要注意</p>
<p>1.调用的方式,POST和GET在cURL里面的请求参数略微有点区别</p>
<p>2.请求的参数,必填的参数必须提交过去</p>
<p>3.请求结果处理</p>
<p>&nbsp;</p>
<p style="text-align: left">&nbsp;</p>
<p style="text-align: left">&nbsp;</p>
<p style="text-align: left">&nbsp;</p><br><br>
来源:https://www.cnblogs.com/chenhanfei/p/11514438.html
頁: [1]
查看完整版本: PHP调用三方平台API的方法