加入收藏 | 设为首页 | 会员中心 | 我要投稿 百客网 - 百科网 (https://www.baikewang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 接口获取数据,php获取api接口数据的方法

发布时间:2022-10-18 10:49:18 所属栏目:PHP教程 来源:
导读:  1、get方式请求api接口

  file_get_contents函数

  $a = file_get_contents(“”);

  如果接口返回json格式的数据,则要转为数组r e = j s o n d e c o d e ( re = json_decode(re=
  1、get方式请求api接口
 
  file_get_contents函数
 
  $a = file_get_contents(“”);
 
  如果接口返回json格式的数据,则要转为数组r e = j s o n d e c o d e ( re = json_decode(re=jsond?ecode(a,true);
 
  2、 post方式请求接口
 
  curl扩展函数
 
  function request_by_curl($remote_server,$post_string,$use_post=true)
 
  { if(function_exists('curl_init')){ $ch = curl_init();
 
  curl_setopt($ch,CURLOPT_URL,$remote_server);
 
  if($use_post){ curl_setopt($ch,CURLOPT_POST, 1);
 
  curl_setopt($ch,CURLOPT_POSTFIELDS,$post_string); }
 
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
 
  $data = curl_exec($ch); curl_close($ch);
 
  return $data; }else{ return '请先安装curl'; }}
 
  $post_string为数组,如array("a"=>1)
 
  3、curl扩展函数也用于get方式请求数据
 
  查看curl是否安装php接口,phpinfo()
 

(编辑:百客网 - 百科网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!