避免api以及项目后台调用数据重复,直接可以调用api信息测试,通过curl模块模拟浏览器行为,方便后台开发:
function get_api_data($api_params,$send_data,$mode = ''){
$api_config = /...../;
$method = /..../;
$api_url = /...../;
if($method && $api_url){
$ch = curl_init();
$query_str = '';
if($method == 'get'){
if($mode == 'json'){
$query_str = '?'.$send_data;
}else{
$query_str = '?'.http_build_query($send_data);
}
}
curl_setopt ($ch, CURLOPT_URL, $api_url.$query_str);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if($method == 'post'){
if($mode == 'json'){
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$send_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: '.strlen($send_data))
);
}else{
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS,http_build_query($send_data));
}
}
$response = curl_exec($ch);
curl_close($ch);
$responseJson = json_decode($response);
return $responseJson;
}else{
return -1; //配置调用出错
}
}