1、第一步安裝postman軟件
2.在右側建一個頁面進行接口測試
左上角選擇數(shù)據(jù)傳輸方式post或者 get ,旁邊寫上接口地址
下方可以選擇傳遞參數(shù)的方式
Params:參數(shù)集合
可以通過右側的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:頭部信息也可以用來參數(shù)
Post傳參數(shù)實列:
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提交的數(shù)據(jù)包
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資源,并且釋放系統(tǒng)資源
$arr_data=objtoarr(json_decode($result));
return $arr_data['access_token'];
}
get傳參數(shù)實列:
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對象轉換成數(shù)組
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;
}