调用API接口

/**

* 调用api接口

* @param url $apiurl api.muxiangdao.cn/Article/articleList

* @param array $param ['status'=>'1','page'=>'2','pageshow'=>'10'];

* @param string $format eg:array(arr),object(obj),json;defalut = array

*/

function get_api($apiurl, $param, $format = 'array'){

if (is_array($param)) {

$string = '?';

foreach ($param as $key => $val){

$string .= $key.'='.$val.'&';

}

$param = substr($string, 0, -1);

}

$url = $apiurl.$param;

$json = get_url($url);

$start = strpos($json, '{');

$end = -1*(strlen(strrchr($json, '}'))-1);

if ($end) {

$json = substr($json, $start, $end);

}else {

$json = substr($json, $start);

}

$obj = json_decode($json);

$array = object_to_array($obj);

$xml = arrayToXml($array);

switch ($format){

case 'array':$data = $array;break;

case 'arr':$data = $array;break;

case 'obj':$data = $obj;break;

case 'object':$data = $obj;break;

case 'json':$data = $json;break;

default:$data = $array;

}

return $data;

}

你可能感兴趣的:(调用API接口)