詳解php用curl調(diào)用接口方法,get和post兩種方式
首先是客戶端執(zhí)行方法ApiModel.php:
<?php /** * 模擬post進(jìn)行url請求 * @param string $url * @param array $post_data */ function request_post($url = '',$ispost=true, $post_data = array()) { if (empty($url) || empty($post_data)) { return false; } $o = ""; foreach ( $post_data as $k => $v ) { $o.= "$k=" . urlencode( $v ). "&" ; } $post_data = substr($o,0,-1); $key=md5(base64_encode($post_data)); if($ispost){ $url=$url; }else{ $url = $url.'?'.$post_data; } $curlPost = 'key='.$key; header("Content-type: text/html; charset=utf-8"); $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$url);//抓取指定網(wǎng)頁 curl_setopt($ch, CURLOPT_HEADER, 0);//設(shè)置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上 if($ispost){ curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); } $data = curl_exec($ch);//運(yùn)行curl curl_close($ch); return $data; } ?>
客戶端調(diào)用方法,可以在此配置基本信息api.php:
<?php require 'ApiModel.php'; function testAction(){ $url = '接口地址'; $post_data['appid'] = '10'; $post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'; $post_data['member_name'] = 'zsjs124'; $post_data['password'] = '123456'; $post_data['email'] = 'zsjs124@126.com'; //$post_data = array(); $res = request_post($url,$ispost=true,$post_data); print_r($res); } testAction(); ?>
服務(wù)器的接口函數(shù)test.php:
<?php function serverapi(){ $key='57173d6ad842d807443ee0db91fed323'; if($_GET&&$_GET['appkey']=='cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'||$_POST&&$_POST['key']===$key){ $arr=array('name'=>'huanglu','password'=>'123456'); echo json_encode($arr); }else{ exit('非法訪問!'); } } serverapi(); ?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP中使用cURL實(shí)現(xiàn)Get和Post請求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php的curl實(shí)現(xiàn)get和post的代碼
- PHP中的使用curl發(fā)送請求(GET請求和POST請求)
- PHP的curl實(shí)現(xiàn)get,post和cookie(實(shí)例介紹)
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實(shí)現(xiàn)Get和Post請求
- PHP中使用CURL發(fā)送get/post請求上傳圖片批處理功能
- php curl發(fā)起get與post網(wǎng)絡(luò)請求案例詳解
- PHP curl get post 請求的封裝函數(shù)示例【get、post、put、delete等請求類型】
相關(guān)文章
PHP如何通過AJAX方式實(shí)現(xiàn)登錄功能
PHP是一門很好的語言,可以很方便的開發(fā)web應(yīng)用程序,下面介紹一下Ajax+PHP+MySQL登陸示例,感興趣的小伙伴們可以參考一下2015-11-11關(guān)于PHP的相似度計(jì)算函數(shù):levenshtein的使用介紹
本篇文章小編將為大家介紹,關(guān)于PHP的相似度計(jì)算函數(shù) levenshtein的使用介紹,有需要的朋友可以參考一下2013-04-04laravel admin實(shí)現(xiàn)分類樹/模型樹的示例代碼
這篇文章主要介紹了laravel admin實(shí)現(xiàn)分類樹/模型樹,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06PHP中使用socket方式GET、POST數(shù)據(jù)實(shí)例
這篇文章主要介紹了PHP中使用socket方式GET、POST數(shù)據(jù)實(shí)例,本文分別給出GET方式和POST方式的代碼實(shí)例,需要的朋友可以參考下2015-04-04淺談laravel orm 中的一對多關(guān)系 hasMany
今天小編就為大家分享一篇淺談laravel orm 中的一對多關(guān)系 hasMany,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP safe_mode開啟對于PHP系統(tǒng)函數(shù)有什么影響
這篇文章主要介紹了PHP safe_mode開啟對于PHP系統(tǒng)函數(shù)有什么影響,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11PHP+MySQL實(shí)現(xiàn)在線測試答題實(shí)例
本文通過實(shí)例主要給大家介紹如何使用jQuery+PHP+MySQL來實(shí)現(xiàn)在線測試題,包括動(dòng)態(tài)讀取題目,答題完畢后臺評分,并返回答題結(jié)果,感興趣的朋友跟隨小編一起看看吧2020-01-01