php查詢笑話大全的方法:1、開通笑話大全接口服務(wù),獲取接口的調(diào)用憑證請求key;2、調(diào)用接口API發(fā)出請求,獲取查詢結(jié)果(json格式);3、使用json_decode()將查詢內(nèi)容解析成數(shù)組;4、打印解析后的內(nèi)容,例“var_dump(結(jié)果數(shù)組);”。
php入門到就業(yè)線上直播課:進入學(xué)習(xí)
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用
本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、DELL G3電腦
基于PHP的笑話大全接口調(diào)用示例
前期準備
-
申請接口
通過
https://www.juhe.cn/docs/api/id/95?s=cpphpcn
自助申請開通接口 -
獲取接口的調(diào)用憑證請求key
可以在個人中心 ➡️ 數(shù)據(jù)中心 ➡️ 我的API 模塊看到此接口的調(diào)用憑證請求key
請仔細閱讀官網(wǎng)的接口文檔,這是聚合數(shù)據(jù)與開發(fā)者的約定,它將有助于您對接口業(yè)務(wù)的理解,從而順利地開展開發(fā)工作
1.隨機獲取笑話接口
請求參數(shù)
名稱 | 必填 | 類型 | 說明 |
---|---|---|---|
key | 是 | string | 在個人中心->我的數(shù)據(jù),接口名稱上方查看 |
代碼示例
//請求的接口URL $apiUrl = 'http://v.juhe.cn/joke/randJoke.php'; //請求參數(shù) $params = [ 'key' => '聚合數(shù)據(jù)上申請的接口調(diào)用key', ]; //參數(shù)數(shù)組轉(zhuǎn)換成字符串 $paramsString = http_build_query($params); //發(fā)起接口網(wǎng)絡(luò)請求 $response = null; try { $response = juheHttpRequest($apiUrl, $paramsString, 1); } catch (Exception $e) { var_dump($e); //此處根據(jù)自己的需求進行具體的異常處理 } if (!$response) { echo '請求異常' . PHP_EOL; } //接收接口返回內(nèi)容 $result = json_decode($response, true); if (!$result) { echo '請求異常' . PHP_EOL; } $errorCode = $result['error_code']; if ($errorCode == 0) { $data = $result['result']; } else { echo "請求異常:{$errorCode}_{$result['reason']}" . PHP_EOL; } //打印接口返回結(jié)果 var_dump($result); /** * 發(fā)起網(wǎng)絡(luò)請求函數(shù) * @param String $url 請求的URL * @param bool $params 請求的參數(shù)內(nèi)容 * @param int $isPost 是否POST請求 * @return bool|string 返回內(nèi)容 */ function juheHttpRequest($url, $params = false, $isPost = 0) { $httpInfo = []; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_TIMEOUT, 12); 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); } } $reponse = curl_exec($ch); if ($reponse === 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 $reponse; }
登錄后復(fù)制
返回結(jié)果示例
array(3) { ["reason"]=> string(7) "success" ["result"]=> array(10) { [0]=> array(3) { ["content"]=> string(395) "【落馬官員的基本特征】 嘴上學(xué)雷鋒,心里學(xué)和珅; 贊揚焦裕祿,看齊雷政富; 都像孔繁森,全是王寶森; 八榮是講話通稿,八恥是行為準則; 以黨性作保證,以異性為獵物; 脫了褲子嫖妓,穿上褲子掃黃; 以國為家,國產(chǎn)變私產(chǎn),愛民若子,民脂刮不止; 白天文明不精神,晚上精神不文明。" ["hashId"]=> string(32) "DE693E907034ADD4ABCF591B8181C13A" ["unixtime"]=> int(1425001334) } [1]=> array(3) { ["content"]=> string(235) "昨晚和老婆吵架了。 老婆:“氣死我了,我要罵人了!” 然后,她對著我家汪汪一個勁的罵啊! 我:“我在這呢!你怎么罵汪汪啊?” 老婆回話了:“你!你還是人嗎!”" ["hashId"]=> string(32) "88C00FC1B66D1EDA7912E54A2D6E0F89" ["unixtime"]=> int(1425001344) } [2]=> array(3) { ["content"]=> string(363) " 一次,我在公園玩悠悠球,一個小朋友跑過來問我:“哥哥,你玩的是什么呀?讓我看看好嗎?“我笑瞇瞇的答應(yīng),把悠悠球給了他,這時那小朋友的媽媽來了,小朋友舉著我的悠悠球?qū)λ麐寢屨f:“媽媽,你看,這是哥哥送我的?!彼麐寢屨f:“那還不快謝謝哥哥!”" ["hashId"]=> string(32) "9779B468CDE58940AABB7E826CB677D0" ["unixtime"]=> int(1425001379) } [3]=> array(3) { ["content"]=> string(243) "家里鬧耗子了,問同事:你家里有貓不,借我養(yǎng)兩天,嚇嚇老鼠。同事賤笑地對我說:不行,是母貓。艸,當哥沒看過段子嗎?回了句:哦,那你留著,自己用。留同事在風(fēng)中凌亂……" ["hashId"]=> string(32) "98b9e3b27c3ac6c44d61ffa84e8a3a11" ["unixtime"]=> int(1425001431) } [4]=> array(3) { ["content"]=> string(837) "無論遇到什么事情;不要輕易說分手;不要輕易放棄感情;更別拿什么緣淺當借口;有時候感情差的就是一個轉(zhuǎn)身而已;下一站未必會更好;感情再深;你不去呵護;慢慢就淡了;許多熟悉的事;你不去回味;漸漸就忘了;微笑;因一個人而起;痛苦;因一個人而生;一次冷落;會悶悶不樂;一句想你;會倍感幸福;所以;當愛在時;就要好好去珍惜;好緣分可遇不可求;有緣的人才能聚首;真感情可守不可丟;有愛的心永不說分手;別把真心愛過;變成曾經(jīng)擁有;別把一次過錯;變成一生錯過;原諒并不難;只要感情深;永遠并不遠,只要彼此真。在一起不是說說而已,而是用心珍惜;真情意不是一時興起;而是一世相依!" ["hashId"]=> string(32) "FC065A2FDFB0E7B1A661C08403C22153" ["unixtime"]=> int(1425001924) } [5]=> array(3) { ["content"]=> string(186) "媽媽給小明煮了兩個雞蛋,但是小明居然只吃了一個。媽媽心說孩子長大了知道心疼媽了。就問小明為啥留一個呢?小明說因為今天是剩蛋節(jié)" ["hashId"]=> string(32) "5DA85D97DA9E99F1C0C433768C50B890" ["unixtime"]=> int(1425001932) } [6]=> array(3) { ["content"]=> string(361) "說個我兄弟的,12年大年三十的晚上,這貨沒事干給10086客服拜年,結(jié)果口才好把人家小姑娘感動哭了,非要給他留電話,這是GC么?當然不是,GC是這小丫頭被查到給用戶留電話被開除了,更GC的是這兩人還在一起了,現(xiàn)在開了個服裝店,很苦也很幸福,今年就要結(jié)婚了……" ["hashId"]=> string(32) "1DDE835A3CCA61D149C0D0B8AA708885" ["unixtime"]=> int(1425001932) } [7]=> array(3) { ["content"]=> string(173) "“你愛我哪一點?”婦人問她的丈夫,“是我的天生麗質(zhì)呢,還是我動人的身軀?” “我最愛你的這些幽默感。”" ["hashId"]=> string(32) "8F0BA8288787959CF6C818FC091128AB" ["unixtime"]=> int(1425001941) } [8]=> array(3) { ["content"]=> string(123) "問:“是不是復(fù)姓的人名字比較上檔次?” 答:“歐陽翠花?”“宇文鐵柱?”“司馬來福?”" ["hashId"]=> string(32) "0CEC9589B4F2C445FC913A79910F9B36" ["unixtime"]=> int(1425002526) } [9]=> array(3) { ["content"]=> string(114) "朋友問我高原上蚊子多不,我回他說,不多,他問為啥,我說,你沒聽說那邊缺癢呀?" ["hashId"]=> string(32) "91834BA3A265DD91527C1B7EB4188052" ["unixtime"]=> int(1425002536) } } ["error_code"]=> int(0) }
登錄后復(fù)制
2.按更新時間查詢笑話
請求參數(shù)
名稱 | 必填 | 類型 | 說明 |
---|---|---|---|
key | 是 | string | 在個人中心->我的數(shù)據(jù),接口名稱上方查看 |
page | 否 | int | 當前頁數(shù),默認1,最大20 |
pagesize | 否 | int | 每次返回條數(shù),默認1,最大20 |
time | 是 | string | 時間戳(10位),如:1418816972 |
代碼示例
<?php //請求的接口URL $apiUrl = 'http://v.juhe.cn/joke/content/list.php'; //請求參數(shù) $params = [ 'key' => '聚合數(shù)據(jù)上申請的接口調(diào)用key', 'sort' => 'desc', 'time' => '1603439247', 'pagesize' => 3 ]; //參數(shù)數(shù)組轉(zhuǎn)換成字符串 $paramsString = http_build_query($params); //發(fā)起接口網(wǎng)絡(luò)請求 $response = null; try { $response = juheHttpRequest($apiUrl, $paramsString, 1); } catch (Exception $e) { var_dump($e); //此處根據(jù)自己的需求進行具體的異常處理 } if (!$response) { echo '請求異常' . PHP_EOL; } //接收接口返回內(nèi)容 $result = json_decode($response, true); if (!$result) { echo '請求異常' . PHP_EOL; } $errorCode = $result['error_code']; if ($errorCode == 0) { $data = $result['result']; } else { echo "請求異常:{$errorCode}_{$result['reason']}" . PHP_EOL; } //打印接口返回結(jié)果 var_dump($result); /** * 發(fā)起網(wǎng)絡(luò)請求函數(shù) * @param String $url 請求的URL * @param bool $params 請求的參數(shù)內(nèi)容 * @param int $isPost 是否POST請求 * @return bool|string 返回內(nèi)容 */ function juheHttpRequest($url, $params = false, $isPost = 0) { $httpInfo = []; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_TIMEOUT, 12); 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); } } $reponse = curl_exec($ch); if ($reponse === 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 $reponse; }
登錄后復(fù)制
返回結(jié)果示例
array(3) { ["reason"]=> string(7) "Success" ["result"]=> array(1) { ["data"]=> array(3) { [0]=> array(4) { ["content"]=> string(294) "某先生是地方上的要人。一天,他像往常一樣在書房里例覽當日報紙,突然對妻子大聲喊道:喂,安娜,你看到今天早報上的流言蜚語了嗎?真可笑!他們說,你收拾行裝出走了。你聽見了嗎?安娜、你在哪兒?安娜???!" ["hashId"]=> string(32) "90B182FC7F74865B40B1E5807CFEBF41" ["unixtime"]=> int(1418745227) ["updatetime"]=> string(19) "2014-12-16 23:53:47" } [1]=> array(4) { ["content"]=> string(192) "有一天我看著報紙,小聲嘟囔著一篇文章的題目鳥兒也有外語,丈夫聽了對了一句:鳥兒當然也有‘外遇’。原來丈夫聽錯了,我笑得前仰后合。" ["hashId"]=> string(32) "206F5C52FD2ED94772CBC66C8AC61F2A" ["unixtime"]=> int(1418745227) ["updatetime"]=> string(19) "2014-12-16 23:53:47" } [2]=> array(4) { ["content"]=> string(363) "新提拔的經(jīng)理覺得從員工中得到的尊重不夠,于是一天戴上一枚自制的徽章,上面寫著:“我是頭兒!”然后在辦公室神氣地來回巡視。中午吃完飯后,經(jīng)理回到辦公室,看見桌上留著一張字條,上面寫著:“你的妻子打來電話,說讓你記著下班后把徽章帶回去,她要用。”" ["hashId"]=> string(32) "B36BF69DC3B622BD8A4F5A7740C31806" ["unixtime"]=> int(1418745227) ["updatetime"]=> string(19) "2014-12-16 23:53:47" } } } ["error_code"]=> int(0) }
登錄后復(fù)制
3.最新笑話
請求參數(shù)
名稱 | 必填 | 類型 | 說明 |
---|---|---|---|
key | 是 | string | 在個人中心->我的數(shù)據(jù),接口名稱上方查看 |
pagesize | 否 | int | 每次返回條數(shù),默認1,最大20 |
page | 否 | int | 當前頁數(shù),默認1,最大20 |
代碼示例
<?php //請求的接口URL $apiUrl = 'http://v.juhe.cn/joke/content/text.php'; //請求參數(shù) $params = [ 'key' => 聚合數(shù)據(jù)上申請的接口調(diào)用key, 'page' => 1, 'pagesize' => 3 ]; //參數(shù)數(shù)組轉(zhuǎn)換成字符串 $paramsString = http_build_query($params); //發(fā)起接口網(wǎng)絡(luò)請求 $response = null; try { $response = juheHttpRequest($apiUrl, $paramsString, 1); } catch (Exception $e) { var_dump($e); //此處根據(jù)自己的需求進行具體的異常處理 } if (!$response) { echo '請求異常' . PHP_EOL; } //接收接口返回內(nèi)容 $result = json_decode($response, true); if (!$result) { echo '請求異常' . PHP_EOL; } $errorCode = $result['error_code']; if ($errorCode == 0) { $data = $result['result']; } else { echo "請求異常:{$errorCode}_{$result['reason']}" . PHP_EOL; } //打印接口返回結(jié)果 var_dump($result); /** * 發(fā)起網(wǎng)絡(luò)請求函數(shù) * @param String $url 請求的URL * @param bool $params 請求的參數(shù)內(nèi)容 * @param int $isPost 是否POST請求 * @return bool|string 返回內(nèi)容 */ function juheHttpRequest($url, $params = false, $isPost = 0) { $httpInfo = []; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_TIMEOUT, 12); 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); } } $reponse = curl_exec($ch); if ($reponse === 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 $reponse; }
登錄后復(fù)制
返回結(jié)果示例
array(3) { ["reason"]=> string(7) "Success" ["result"]=> array(1) { ["data"]=> array(3) { [0]=> array(4) { ["content"]=> string(335) " 小區(qū)門口修車師傅生意特好。人實在,只要不換零件,常常不收錢或只收個塊把錢辛苦錢。最近發(fā)現(xiàn)他心黑了,價錢明顯上調(diào),就連充個氣也收1塊錢。旁邊書報亭大媽是知情人,: 唉,多擔(dān)待一下吧!他家上個月二胎,一窩生了四個帶把的。。。。" ["hashId"]=> string(32) "2e010657b420dcee335c870bf9a18301" ["unixtime"]=> int(1559789702) ["updatetime"]=> string(19) "2019-06-06 10:55:02" } [1]=> array(4) { ["content"]=> string(246) "前天和同事去逛超市,說好久沒買肉吃了,于是去看豬肉,結(jié)果看到的最便宜的豬肉都要十八塊多,同事當場驚呼:“豬怎么了,這是!”賣肉的師傅笑著說:“豬沒事,就是漲價了?!?quot; ["hashId"]=> string(32) "d261d1f0d76bdc665d80f104cf7a7d65" ["unixtime"]=> int(1556845202) ["updatetime"]=> string(19) "2019-05-03 09:00:02" } [2]=> array(4) { ["content"]=> string(518) "去小賣部買煙,10塊錢一盒的,給老板100,老板喜咪咪的看著我說:“我沒零錢找你,就讓我閨女陪你一晚抵賬得了?!蔽乙患樱骸澳憷蟿e介啊,我攢個私房錢出來偷偷買煙,真的很不容易??!”老板:“怎么,你不愿意?要不然我去把我閨女叫出來!”我:“別!別!這錢你不用找了,千萬別讓您閨女知道我藏私房錢??!”老板:“好女婿,只要你經(jīng)常來買東西,我保證不說!哈哈哈”" ["hashId"]=> string(32) "7d6e3f6cda682c18c3bc122a767b8756" ["unixtime"]=> int(1556845202) ["updatetime"]=> string(19) "2019-05-03 09:00:02" } } } ["error_code"]=> int(0) }
登錄后復(fù)制
推薦學(xué)習(xí):《PHP視頻教程》