PHP 傳輸會(huì)話curl函數(shù)的實(shí)例詳解
PHP 傳輸會(huì)話curl函數(shù)的實(shí)例詳解
前言:
接手公司項(xiàng)目PC端負(fù)責(zé)人的重?fù)?dān),責(zé)任擔(dān)當(dāng)重大;從需求分析,畫流程圖,建表,編碼,測(cè)試修bug,上線維護(hù)等我一個(gè)光桿司令一人完成(當(dāng)然還有一個(gè)技術(shù)不錯(cuò)的前端配合,感謝主管的幫助),雖然累點(diǎn)加班多點(diǎn)但感覺還行吧,公司都是一個(gè)鳥樣。
閑話不多說了,因?yàn)轫?xiàng)目中經(jīng)常需要調(diào)取java那邊的接口,既然涉及到請(qǐng)求接口那就有了http的請(qǐng)求方式,PHP常見的是GET/POST兩種當(dāng)然還有其他的比如put等,java那邊經(jīng)常用到GET/POST/PUT/DELETE等方式,請(qǐng)求接口當(dāng)然要用到curl的相關(guān)函數(shù)了,都是看文檔調(diào)試的希望大家都看文檔,下面是我封裝好的相關(guān)函數(shù)等(大概總結(jié)下,已調(diào)通):
示例代碼:
private $serverhost = "https://demo.xxx.cn"; //測(cè)試
/**
* 請(qǐng)求接口封裝 get/post/put/delete等
* access public
* @param string $url 接口地址
* @param string $params 參數(shù)
* @param string $type 類型 get/post/put/delete
* @return bool/array
*/
public function getcurldata($url,$params,$type="get"){
$url = $this->serverhost.$url;
$response = array();
if($type == 'get'){ //get請(qǐng)求
//請(qǐng)求頭可以加其他設(shè)置
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
}elseif ($type == 'post'){ //post請(qǐng)求
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true); //注意這幾行
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //注意這幾行
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
}elseif ($type == 'put'){ //put請(qǐng)求
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PUT, true); //注意這幾行
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
//curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
}
return $response;
}
//如何調(diào)用上面代碼
//get方式
/**
* 查詢我創(chuàng)建過的班級(jí)
* @param string $url 接口地址
* @param string $params 參數(shù)
* @param string $type 類型 get
* @return array
*/
public function mycreateclass($userid){
$url = "/xxx/xxxx/xxxx/".$userid; //請(qǐng)求地址拼接
$response = $this->getcurldata($url,array(),"get");
$createdclass = json_decode($response, true); //返回json格式數(shù)據(jù)
return $createdclass;
}
/** post方式請(qǐng)求
* 用戶登錄判斷
* access public
* @param string $username 用戶名
* @param string $password 密碼
* @return bool
*/
public function getlogin($username,$password)
{
//要post的數(shù)據(jù)
$params = array(
"username" => $username,
"password" => $password
);
$params = json_encode($params, 64|256);
$uri = "/xxx/xxx/login";
$response = $this->getcurldata($uri,$params,"post");
$result = json_decode($response, true);
return $result ;
}
/*身份轉(zhuǎn)換--put 請(qǐng)求
*/
public function changeuserole($token){
//要put的數(shù)據(jù)
$params = array();
$params = json_encode($params, 64|256);
$uri = "/xxx/xxx/xxx/".$token."/";
$response = $this->getcurldata($uri,$params,"put");
$result = json_decode($response, true);
//dump($result);die;
return $result;
}
//還有一個(gè)delete方式 大家自己參考文檔調(diào)試下吧
上面3個(gè)請(qǐng)求方式都是單次請(qǐng)求(即請(qǐng)求一次)***************
PHP自帶函數(shù)curl_multi_get_contents函數(shù)(thinkphp自帶此函數(shù),可以微調(diào)下):
/**
* 批量發(fā)起請(qǐng)求 已調(diào)通
* curl multi POST數(shù)據(jù)
* */
public function curl_multi_get_contents($url=array(), $param = array(), $timeout=1000){
$userAgent = 'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)';
$headers = array(
'Content-type: application/json;charset=UTF-8',
);
$curl_array=array();
$mh = curl_multi_init();
foreach($url as $uk=>$uv){
$curl_array[$uk] = curl_init();
}
unset($uk,$uv);
foreach($url as $uk=>$uv) {
$options = array(
CURLOPT_URL => $uv,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_DNS_CACHE_TIMEOUT => 86400,
CURLOPT_DNS_USE_GLOBAL_CACHE => true,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HEADER => false,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
CURLOPT_HTTPHEADER => $headers,
);
if (isset($param[$uk])) {
foreach ($param[$uk] as $_k => $_v) {
//$options[$_k] = $_v;
$optionsparam[$_k] = $_v;
$options[CURLOPT_POSTFIELDS] = json_encode($optionsparam, 64|256);
}
}
curl_setopt_array($curl_array[$uk], $options);
curl_multi_add_handle($mh, $curl_array[$uk]);
unset($options);
}
unset($uk,$uv);
$running = NULL;
do {
curl_multi_exec($mh,$running);
} while($running > 0);
$res = array();
foreach($url as $uk=>$uv){
$res[$uk] = curl_multi_getcontent($curl_array[$uk]);
}
unset($uk,$uv);
foreach($url as $uk=>$uv){
curl_multi_remove_handle($mh, $curl_array[$uk]);
}
unset($url,$curl_array,$uk,$uv);
curl_multi_close($mh);
return $res;
}
//如何調(diào)用--批量發(fā)起請(qǐng)求
//批量請(qǐng)求加入班級(jí)
public function batchjoinclass($token,$batchjoinclass){
$urlarr = $param = $returndata = array();
$param = $batchjoinclass; //二維數(shù)組 格式如下
/*
$param[1]['name'] = '班級(jí)新1';
$param[1]['iamge'] = 'xxx11.png';
$param[1]['iamgeType'] = 'CUSTOM';
$param[2]['name'] = '班級(jí)新2';
$param[2]['iamge'] = 'xxx.png';
$param[2]['iamgeType'] = 'CUSTOM';
*/
//獲取請(qǐng)求url
foreach($batchjoinclass as $key=>$val){
$urlarr[$key] = $this->serverhost."/xxx/xxxx/xxxx/".$token;
}
$res = $this->curl_multi_get_contents($urlarr,$param);
//格式化返回?cái)?shù)據(jù)
foreach($res as $key=>$val){
$returndata[$key] = json_decode($val,true);
}
return $returndata;
}
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
使用JavaScript創(chuàng)建新樣式表和新樣式規(guī)則
這篇文章主要介紹了使用JavaScript創(chuàng)建新樣式表和新樣式規(guī)則的相關(guān)資料,需要的朋友可以參考下2016-06-06
動(dòng)態(tài)表單驗(yàn)證的操作方法和TP框架里面的ajax表單驗(yàn)證
這篇文章主要介紹了動(dòng)態(tài)表單驗(yàn)證的操作方法和TP框架里面的ajax表單驗(yàn)證問題,需要的朋友可以參考下2017-07-07
php設(shè)計(jì)模式之代理模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之代理模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP設(shè)計(jì)模式中代理模式的相關(guān)原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03

