詳解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全局變量中的global與$GLOBALS的區(qū)別和用法小結(jié)
這篇文章主要介紹了PHP全局變量中的global與$GLOBALS的區(qū)別和用法小結(jié),global的作用就相當(dāng)于傳遞參數(shù),在函數(shù)外部聲明的變量,如果在函數(shù)內(nèi)想要使用,就用global來聲明該變量,這樣就相當(dāng)于把該變量傳遞進(jìn)來了,就可以引用該變量了,需要的朋友可以參考下2023-10-10
php實(shí)現(xiàn)兼容2038年后Unix時間戳轉(zhuǎn)換函數(shù)
這篇文章主要介紹了php實(shí)現(xiàn)兼容2038年后Unix時間戳轉(zhuǎn)換函數(shù),使用方法和就的函數(shù)一樣,非常實(shí)用,推薦給大家,希望大家能夠喜歡。2015-03-03
利用yahoo匯率接口實(shí)現(xiàn)實(shí)時匯率轉(zhuǎn)換示例 匯率轉(zhuǎn)換器
這篇文章主要介紹了利用yahoo匯率接口實(shí)現(xiàn)實(shí)時匯率轉(zhuǎn)換示例,大家參考使用吧2014-01-01
thinkPHP5框架auth權(quán)限控制類與用法示例
這篇文章主要介紹了thinkPHP5框架auth權(quán)限控制類與用法,結(jié)合實(shí)例形式分析了thinkPHP5框架擴(kuò)展auth權(quán)限控制類的定義與使用方法,代碼注釋中備有較為詳盡的使用說明與數(shù)據(jù)庫操作語句,需要的朋友可以參考下2018-06-06
Aliyun Linux 編譯安裝 php7.3 tengine2.3.2 mysql8.0 redis5的過程詳解
這篇文章主要介紹了Aliyun Linux 編譯安裝 php7.3 tengine2.3.2 mysql8.0 redis5,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

