首頁  >  文章  >  後端開發  >  php怎麼查詢星座運勢

php怎麼查詢星座運勢

藏色散人
藏色散人原創
2022-10-24 17:22:241472瀏覽

php查詢星座運勢的方法:1、開通星座運勢API介面;2、建立PHP範例檔案;3、請求介面URL;4、設定申請的appkey;5、發起介面網路請求;6、透過「function juhecurl($url,$params=false,$ispost=0){...}」方式請求介面返回內容,再根據業務實際邏輯調整修改即可。

php怎麼查詢星座運勢

本教學操作環境:windows7系統、PHP8.1版、Dell G3電腦。

php怎麼查詢星座運勢?

1、開通星座運勢API介面:

透過https://www.juhe.cn/docs/api/id/58 ?s=cpphpcn註冊及開通

介面說明:(十二星座的今日運勢)

  • 12星座運勢解析

  • 解析內容全面,有今日運勢解析,明日運勢解析及本週運勢解析

  • 解析內容新穎,個人運勢解析,貴人運勢分析,需提防事宜等。

2、基於php的星座運勢介面呼叫範例

程式碼範例:

// 星座运势调用示例代码 

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;

}

推薦學習:《PHP影片教學

以上是php怎麼查詢星座運勢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn