課程
/后端開發
/PHP
/PHP微信公眾平臺開發高級篇—生成二維碼接口
json ?怎么傳的
2016-12-09
源自:PHP微信公眾平臺開發高級篇—生成二維碼接口 1-2
正在回答
/*
? ? ? ? $url ?接口url string
? ? ? ? $type 請求類型 strin?
? ? ? ? $res ?返回數據類型 string?
? ? ? ? $arr ?post請求參數 string
? ? */
? ? public function http_curl($url,$type='get',$res='json',$arr=''){
? ? ? ? //1.初始化curl
? ? ? ? $ch=curl_init();
? ? ? ? //2.設置curl的參數
? ? ? ? curl_setopt($ch, CURLOPT_URL, $url);
? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
? ? ? ? //判斷是否為post請求
? ? ? ? if($type == 'post'){
? ? ? ? ? ? curl_setopt($ch, CURLOPT_POST, 1);
? ? ? ? ? ? curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
? ? ? ? }
? ? ? ? ?// 跳過證書檢查?
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
? ? ? ? // 從證書中檢查SSL加密算法是否存在 ? ?
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);?
? ? ? ? //3.采集
? ? ? ? $output=curl_exec($ch);
? ? ? ? //4.關閉
? ? ? ? curl_close($ch);
? ? ? ? //如果為json格式轉換
? ? ? ? if($res == 'json'){
? ? ? ? ? ? //如果產生錯誤
? ? ? ? ? ? if(curl_errno($ch)){
? ? ? ? ? ? ? ? //請求失敗
? ? ? ? ? ? ? ? return curl_error($ch);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? //請求成功
? ? ? ? ? ? ? ? return json_decode($output,true);
? ? ? ? ? ? }
? ? }
舉報
微信掃描,值得你擁有,本教程主要講解二維碼接口的實現
1 回答關注與未關注的EventKry值
1 回答怎么查看文檔
2 回答怎么連接到數據庫?
1 回答求助課程源碼
7 回答為啥我跟老師一模一樣的代碼,http_curl返回的是空值?有跟我遇到一樣問題的嗎?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-12-11
/*
? ? ? ? $url ?接口url string
? ? ? ? $type 請求類型 strin?
? ? ? ? $res ?返回數據類型 string?
? ? ? ? $arr ?post請求參數 string
? ? */
? ? public function http_curl($url,$type='get',$res='json',$arr=''){
? ? ? ? //1.初始化curl
? ? ? ? $ch=curl_init();
? ? ? ? //2.設置curl的參數
? ? ? ? curl_setopt($ch, CURLOPT_URL, $url);
? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
? ? ? ? //判斷是否為post請求
? ? ? ? if($type == 'post'){
? ? ? ? ? ? curl_setopt($ch, CURLOPT_POST, 1);
? ? ? ? ? ? curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
? ? ? ? }
? ? ? ? ?// 跳過證書檢查?
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
? ? ? ? // 從證書中檢查SSL加密算法是否存在 ? ?
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);?
? ? ? ? //3.采集
? ? ? ? $output=curl_exec($ch);
? ? ? ? //4.關閉
? ? ? ? curl_close($ch);
? ? ? ? //如果為json格式轉換
? ? ? ? if($res == 'json'){
? ? ? ? ? ? //如果產生錯誤
? ? ? ? ? ? if(curl_errno($ch)){
? ? ? ? ? ? ? ? //請求失敗
? ? ? ? ? ? ? ? return curl_error($ch);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? //請求成功
? ? ? ? ? ? ? ? return json_decode($output,true);
? ? ? ? ? ? }
? ? ? ? }
? ? }