Tp-admin开发框架,不多做介绍。目前已经在很多项目中得到了验证。开源地址:https://gitee.com/zkii_admin/Tp-admin
本篇笔记的代码片段主要为了弥补HttpService中请求接口时对basicAuth的支持。
代码片段:
- /**
- * POST 请求(支持Basic认证)
- * @param string $url HTTP请求URL地址
- * @param array|string $data POST提交的数据
- * @param int $second 请求超时时间
- * @param array $header 请求Header信息
- * @param array $auth Basic认证信息
- * @return bool|string
- */
- static public function basicPost($url, $data = [], $second = 30, $header = [],$auth=[]){
- $curl = curl_init();
- self::applyData($data);
- self::applyHttp($curl, $url);
- curl_setopt($curl, CURLOPT_TIMEOUT, $second);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- if (!emptyempty($header)) {
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- }
- if(!emptyempty($author)){
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, $auth['username'].":".$auth['password']);
- }
- list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
- return (intval($status["http_code"]) === 200) ? $content : false;
- }