How to query horoscope in php: 1. Open the horoscope API interface; 2. Create a PHP sample file; 3. Request the interface URL; 4. Configure the applied appkey; 5. Initiate an interface network request; 6. Use "function juhecurl($url,$params=false,$ispost=0){...}" to request the interface to return content, and then adjust and modify it according to the actual business logic.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How to check horoscope in php?
1. Open the horoscope API interface:
viahttps://www.juhe.cn/docs/api/id/58 ?s=cpphpcn
Registration and activation
Interface description: (Today’s fortune of the twelve zodiac signs)
12 horoscope analysis
The analysis content is comprehensive, including today's fortune analysis, tomorrow's fortune analysis and this week's fortune analysis
The analysis content is novel, personal fortune analysis, noble person's fortune analysis, matters needing attention, etc. .
2. PHP-based horoscope interface call example
Code example:
// 星座运势调用示例代码 header('Content-type:text/html;charset=utf-8'); //配置您申请的appkey $appkey = "*********************"; //************1.运势查询************ $url = "http://web.juhe.cn:8080/constellation/getAll"; $params = array( "key" => $appkey,//应用APPKEY(应用详细页查询) "consName" => "",//星座名称,如:白羊座 "type" => "",//运势类型:today,tomorrow,week,nextweek,month,year ); $paramstring = http_build_query($params); $content = juhecurl($url,$paramstring); $result = json_decode($content,true); if($result){ if($result['error_code']=='0'){ print_r($result); }else{ echo $result['error_code'].":".$result['reason']; } }else{ echo "请求失败"; } //************************************************** /** * 请求接口返回内容 * @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; }
Recommended study:《PHP video tutorial》
The above is the detailed content of How to check horoscope in php. For more information, please follow other related articles on the PHP Chinese website!