詳解php用curl調(diào)用接口方法,get和post兩種方式
更新時間:2017年01月13日 17:13:20 作者:屌絲IT男
本篇文章主要介紹了詳解php用curl調(diào)用接口方法,get和post兩種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
首先是客戶端執(zhí)行方法ApiModel.php:
<?php /** * 模擬post進行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);//運行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實現(xiàn)Get和Post請求的方法
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- php的curl實現(xiàn)get和post的代碼
- PHP中的使用curl發(fā)送請求(GET請求和POST請求)
- PHP的curl實現(xiàn)get,post和cookie(實例介紹)
- php使用CURL模擬GET與POST向微信接口提交及獲取數(shù)據(jù)的方法
- PHP CURL模擬GET及POST函數(shù)代碼
- PHP如何使用cURL實現(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)文章
關(guān)于PHP的相似度計算函數(shù):levenshtein的使用介紹
本篇文章小編將為大家介紹,關(guān)于PHP的相似度計算函數(shù) levenshtein的使用介紹,有需要的朋友可以參考一下2013-04-04laravel admin實現(xiàn)分類樹/模型樹的示例代碼
這篇文章主要介紹了laravel admin實現(xiàn)分類樹/模型樹,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06PHP中使用socket方式GET、POST數(shù)據(jù)實例
這篇文章主要介紹了PHP中使用socket方式GET、POST數(shù)據(jù)實例,本文分別給出GET方式和POST方式的代碼實例,需要的朋友可以參考下2015-04-04淺談laravel orm 中的一對多關(guān)系 hasMany
今天小編就為大家分享一篇淺談laravel orm 中的一對多關(guān)系 hasMany,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP safe_mode開啟對于PHP系統(tǒng)函數(shù)有什么影響
這篇文章主要介紹了PHP safe_mode開啟對于PHP系統(tǒng)函數(shù)有什么影響,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11