1、第一步安装postman软件
2.在右侧建一个页面进行接口测试
左上角选择数据传输方式post或者 get ,旁边写上接口地址
下方可以选择传递参数的方式
Params:参数集合
可以通过右侧的code功能查看代码
可以选择Java,php,js等待语言,省去了写代码的时间(比如这个php代码复制下来就能用)
<?php
$curl = curl_init();
curl_setopt_array($curl,
array(
CURLOPT_URL => 'url',
CURLOPT_RETURNTRANSFER =>
true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION =>
true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER =>
array(
'Authorization: Bearer 00D8c000003UHSM!AQYAQD2poDyZp1kil88kHO2rGvpyjDSApjrNdFdkckd3.gkEBn__x4EIm3JG2cY4CoS.hCn2TNNYgYH.9JJ88N_bg4nFwPNt',
'Cookie: BrowserId=DvcrXyzNEeycjRMjkaKHfA;CookieConsentPolicy=0:1;LSKey-c$CookieConsentPolicy=0:1'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Authorization:描述获取授权的方式
Header:头部信息也可以用来参数
Post传参数实列:
function get_api_token($url,$data){
$ch = curl_init(); // 初始一个curl会话
$timeout = 30; // php运行超时时间,单位秒
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url); // 设置url
curl_setopt($ch, CURLOPT_POST, 1); // post 请求
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array(
'Content-Type: application/x-www-form-urlencoded',
)); // 一定要定义content-type为xml,要不然默认是text/html!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post提交的数据包
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // PHP脚本在成功连接服务器前等待多久,单位秒
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// 是否报错
if(curl_errno($ch))
{
return curl_error($ch);
}
//dump($result);exit;
curl_close($ch); // //关闭cURL资源,并且释放系统资源
$arr_data=objtoarr(json_decode($result));
return $arr_data['access_token'];
}
get传参数实列:
function curl_get($no,$token){
//dump($no);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER=>0,
CURLOPT_SSL_VERIFYHOST=>0,
CURLOPT_TIMEOUT => 0,
CURLOPT_HEADER=>false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$token
),
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if(curl_errno($curl))
{
return curl_error($curl);
}
curl_close($curl);
//dump($response);
$result=objtoarr(json_decode($response));
return $result;
}
将返回的json对象转换成数组
function objtoarr($obj){
$ret = array();
foreach($obj as $key =>$value){
if(gettype($value) == 'array' || gettype($value) == 'object'){
$ret[$key] = objtoarr($value);
}else{
$ret[$key] = $value;
}
}
return $ret;
}