$appkey,//APP Key
“type” => “”,//两种格式(0或者1,默认为0)
“bank” => “5”,//两种格式(0或者1,默认为0)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
if($result[‘error_code’]==’0′){
$response = returnTable($result[‘result’][‘0’]);
echo $response;
}else{
echo $result[‘error_code’].”:”.$result[‘reason’];
}
}else{
echo “请求失败”;
}
/**
* @param $tableArr 处理的数组
* @return string 输出HTML
*/
function returnTable($tableArr){
$html = ”;
$th = ‘
‘;
$html = ‘
{$val[‘name’]} | ” . replaceName($val[‘name’]) . “ | {$val[‘fBuyPri’]} | {$val[‘fSellPri’]} | {$val[‘date’]} | {$val[‘time’]} |
‘;
return $html;
}
/**
* @param $name
* @return string
*/
function replaceName($name){
$response = ”;
switch($name){
case ‘美元’:
$response = ‘USD/CNY’;
break;
case ‘欧元’:
$response = ‘EUR/CNY’;
break;
case ‘港币’:
$response = ‘HKD/CNY’;
break;
case ‘日元’:
$response = ‘JPY/CNY’;
break;
case ‘英镑’:
$response = ‘GBP/CNY’;
break;
case ‘澳大利亚元’:
$response = ‘AUD/CNY’;
break;
case ‘加拿大元’:
$response = ‘CAD/CNY’;
break;
case ‘泰国铢’:
$response = ‘THB/CNY’;
break;
case ‘新加坡元’:
$response = ‘SGD/CNY’;
break;
case ‘瑞典克朗’:
$response = ‘SEK/CNY’;
break;
case ‘瑞士法郎’:
$response = ‘CHF/CNY’;
break;
case ‘丹麦克朗’:
$response = ‘DKK/CNY’;
break;
case ‘挪威克朗’:
$response = ‘NOK/CNY’;
break;
case ‘南非兰特’:
$response = ‘ZAR/CNY’;
break;
case ‘卢布’:
$response = ‘RUB/CNY’;
break;
case ‘澳门元’:
$response = ‘MOP/CNY’;
break;
case ‘菲律宾比索’:
$response = ‘PHP/CNY’;
break;
case ‘新西兰元’:
$response = ‘NZD/CNY’;
break;
case ‘林吉特’:
$response = ‘MYR/CNY’;
break;
case ‘韩国元’:
$response = ‘KRW/CNY’;
break;
case ‘新台币’:
$response = ‘TWD/CNY’;
break;
default:
$response = ”;
break;
}
return $response;
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , ‘JuheData’ );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.’?’.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo “cURL Error: ” . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
} ?>