{ "error_code": 0, "reason": "Success", "result": { "data": [{ "content": "有一天晚上我俩一起吃西瓜,老大把西瓜籽很整洁的吐在了一张纸上, 过了几天,我从教室回但宿舍看到老大在磕瓜子, 我就问他:老大,你什么时候买的瓜子? 老大说:刚晒好,说着抓了一把要递给我……", "hashId": "bcc5fdc2fb6efc6db33fa242474f108a", "unixtime": 1418814837, "updatetime": "2014-12-17 19:13:57" } ] } }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/3/15 17:50 */ //---------------------------------- // 聚合 笑话大全 调用类 //---------------------------------- class freeApi{ private $apiKey = false; //聚合appkey private $apiUrl = 'http://v.juhe.cn/joke/content/list.php'; public function __construct($apikey){ $this->apiKey = $apikey; } /** * 将JSON内容转为数据,并返回 * @param string $content [内容] * @return array */ public function returnArray($content){ return json_decode($content,true); } /** * 获取 笑话大全 结果 * @return array */ public function getResult(){ $params = [ "sort" => "",//类型,desc:指定时间之前发布的,asc:指定时间之后发布的 "page" => "",//当前页数,默认1 "pagesize" => "",//每次返回条数,默认1,最大20 "time" => "",//时间戳(10位),如:1418816972 "key" => $this->apiKey,//您申请的key ]; $params = http_build_query($params); return $this->returnArray($this->freeApiCurl($this->apiUrl,$params,1)); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($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_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'chuanshuoapi' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , 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) { return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; } }