聚合数据接口调用实例
//控制器
class SignController extends Controller
{
const SIGNARRAY=[\'1\'=>\'白羊座\',2=>\'金牛座\',3=>\'双子座\',4=>\'巨蟹座\',5=>\'狮子座\',6=>\'处女座\',7=>\'天秤座\',8=>\'天蝎座\',9=>\'射手座\',10=>\'魔羯座\',11=>\'水瓶座\',12=>\'双鱼座\'];
public function index()
{
return view(\'admin.sign.index\');
}
public function getData(Request $request)
{
$appkey="你的安排appKey";
$url="http://web.juhe.cn:8080/constellation/getAll";
$sign=trim(self::SIGNARRAY[$request->get(\'sign\')]);
$range=trim($request->get(\'range\'));
$params = array(
"key" => $appkey,//应用APPKEY(应用详细页查询)
"consName" => $sign,//星座名称,如:白羊座
"type" => $range,//运势类型:today,tomorrow,week,nextweek,month,year
);
$paramstring = http_build_query($params);
$content=$this->juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
if($result[\'error_code\']==\'0\'){
$data=
\'请求时间:\'.$result[\'datetime\'].\'<br/>\'.
\'星座:\'.$result[\'name\'].\'<br/>\'.
\'综合指数:\'.$result[\'all\'].\'<br/>\'.
\'幸运色:\'.$result[\'color\'].\'<br/>\'.
\'健康指数:\'.$result[\'health\'].\'<br/>\'.
\'爱情指数:\'.$result[\'love\'].\'<br/>\'.
\'财运指数:\'.$result[\'money\'].\'<br/>\'.
\'幸运数字:\'.$result[\'number\'].\'<br/>\'.
\'速配星座:\'.$result[\'QFriend\'].\'<br/>\'.
\'工作指数:\'.$result[\'work\'].\'<br/>\'.
\'今日概述:\'.$result[\'summary\'].\'<br/>\'
;
return response()->json([\'status\'=>\'ok\',\'msg\'=>\'请求成功\',\'data\'=>$data]);
}else{
echo $result[\'error_code\'].":".$result[\'reason\'];
}
}else{
return response()->json([\'status\'=>\'error\',\'msg\'=>\'请求失败\',\'data\'=>$result]);
}
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
public function juhecurl($url,$params=false,$ispost=0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );//设置curl http传输协议强制使用http/1.1
curl_setopt( $ch, CURLOPT_USERAGENT , \'JuheData\' );//在HTTP请求中包含一个"User-Agent: "头的字符串。
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );//在发起连接前等待的时间,如果设置为0,则无限等待。
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);//设置cURL允许执行的最长秒数。
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );//将curl_exec()获取的信息以文件流的形式返回不输出
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//启用之后会将服务器返回的Location:放在header中递归
//的形式返回给服务器。
if( $ispost )
{
//发送post请求
curl_setopt( $ch , CURLOPT_POST , true );//启用后会发送一个常规的post请求,类型为
//application/x-www-form-urlencoded,就想表单提交的一样
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );//全部数据使用http协议中的POST操作来发送。
curl_setopt( $ch , CURLOPT_URL , $url );//访问那个url
}
else
{
//发送get请求
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 );//获取一个CURL连接资源句柄的信息
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
}