聚合数据接口调用实例

//控制器

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;
}
}