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