<?php
//Ajax方式返回数据到客户端
function ajax_echo($data,$exit=true,$type='JSON') {
$echo='';
switch (strtoupper($type)){
case 'JSON' :
$echo=encode_json($data);
if(!$exit) return $echo;
header('Content-Type:application/json; charset=utf-8');
exit($echo);
case 'JSONP':
$echo ='callback('.encode_json($data).');';
if(!$exit) return $echo;
header('Content-Type:application/json; charset=utf-8');
exit($echo);
case 'XML' :
$echo=encode_xml($data);
if(!$exit) return $echo;
header('Content-Type:text/xml; charset=utf-8');
exit($echo);
case 'HTML' :
if(!$exit) return $data;
header('Content-Type:text/html; charset=utf-8');
exit($data);
default :
throw new Exception("ajax_echo undefind type", 1);
}
}
//处理xml
function encode_xml($data, $root='xml', $attr='', $item='item', $id='id', $encoding='utf-8') {
if(is_array($attr)){
$_attr = array();
foreach ($attr as $key => $value) {
$_attr[] = "{$key}=\"{$value}\"";
}
$attr = implode(' ', $_attr);
}
$attr = trim($attr);
$attr = empty($attr) ? '' : " {$attr}";
/*
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
*/
$xml = '';
$xml .= "<{$root}{$attr}>";
$xml .= data_to_xml($data, $item, $id);
$xml .= "</{$root}>";
return $xml;
}
function data_to_xml($data, $item='item', $id='id') {
$xml = $attr = '';
foreach ($data as $key => $val) {
if(is_numeric($key)){
$id && $attr = " {$id}=\"{$key}\"";
$key = $item;
}
$xml .= "<{$key}{$attr}>";
$xml .= (is_array($val) || is_object($val)) ? data_to_xml($val, $item, $id) : $val;
$xml .= "</{$key}>";
}
return $xml;
}
//处理json
function encode_json($str) {
if(version_compare(PHP_VERSION,'5.4.0','>')){
return json_encode($str,JSON_UNESCAPED_UNICODE);
}else{
return urldecode(json_encode(url_encode($str)));
}
}
function url_encode($str) {
if(is_array($str)) {
foreach($str as $key=>$value) {
if(is_string($key)) $key=urlencode($key);
$str[$key] = url_encode($value);
}
} else {
if(is_string($str)) $str= urlencode($str);
}
return $str;
}
//发起http请求
function http_curl($url, $param='',$method = 'GET', $data = '',$header=array()){
$opts = array(
CURLOPT_TIMEOUT => 30,//设置请求超时时间
CURLOPT_RETURNTRANSFER => 1,//获取的信息以文件流的形式返回,而不是直接输出
CURLOPT_SSL_VERIFYPEER => false,//禁用后cURL将终止从服务端进行验证
CURLOPT_SSL_VERIFYHOST => false,//检查服务器SSL证书中是否存在一个公用名
);
if(!empty($param)){
if(!is_string($param)) $param=http_build_query($param);
$url.=$param;
}
$opts[CURLOPT_URL] = $url;
if(strtoupper($method) == 'POST' ){
$opts[CURLOPT_POST] = 1;
if(!empty($data)){
if(!is_string($data)) $data=http_build_query($data);
$opts[CURLOPT_POSTFIELDS] = $data;
}
}
if(!empty($header)){
$opts[CURLOPT_HTTPHEADER]=$header;
// $opts[CURLOPT_HTTPHEADER] = array(
// 'Content-Type: application/json; charset=utf-8',
// 'Content-Length: ' . strlen($data),
// );
}
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
//发生错误,抛出异常
if($error) throw new Exception('请求发生错误:' . $error);
return $data;
}
/*
CURL
curl_setopt($ch, CURLOPT_HEADER, 1);//获取头信息
curl_setopt($ch, CURLOPT_NOBODY, 1);//不获取主体内容
get_headers($url,true);//获取头信息
*/
转载于:https://my.oschina.net/miaowang/blog/639349