1、第一步安裝postman軟件
2.在右側(cè)建一個(gè)頁面進(jìn)行接口測(cè)試
左上角選擇數(shù)據(jù)傳輸方式post或者 get ,旁邊寫上接口地址
下方可以選擇傳遞參數(shù)的方式
Params:參數(shù)集合
可以通過右側(cè)的code功能查看代碼
可以選擇Java,php,js等待語言,省去了寫代碼的時(shí)間(比如這個(gè)php代碼復(fù)制下來就能用)
<?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:描述獲取授權(quán)的方式
Header:頭部信息也可以用來參數(shù)
Post傳參數(shù)實(shí)列:
function get_api_token($url,$data){
$ch = curl_init(); // 初始一個(gè)curl會(huì)話
$timeout = 30; // php運(yùn)行超時(shí)時(shí)間,單位秒
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_URL, $url); // 設(shè)置url
curl_setopt($ch, CURLOPT_POST, 1); // post 請(qǐng)求
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,要不然默認(rèn)是text/html!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post提交的數(shù)據(jù)包
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // PHP腳本在成功連接服務(wù)器前等待多久,單位秒
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// 是否報(bào)錯(cuò)
if(curl_errno($ch))
{
return curl_error($ch);
}
//dump($result);exit;
curl_close($ch); // //關(guān)閉cURL資源,并且釋放系統(tǒng)資源
$arr_data=objtoarr(json_decode($result));
return $arr_data['access_token'];
}
get傳參數(shù)實(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對(duì)象轉(zhuǎn)換成數(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;
}
如沒特殊注明,文章均為方維網(wǎng)絡(luò)原創(chuàng),轉(zhuǎn)載請(qǐng)注明來自http://m.oulysa.com/news/6537.html