php7 curl 发起post、get请求、post raw形式上传文件函数体封装
2022-07-28 15:20:24
327
{{single.collect_count}}

发起post、get请求

/** * 发起请求 * @param string $url * @param string|array $ret请求体 **/private function http_req($url, $ret = ''){// 初始化$ch = curl_init();// 相关设置curl_setopt($ch, CURLOPT_URL, $url);// 不要请求头curl_setopt($ch, CURLOPT_HEADER, 0);// 请求结果不直接输出,而是字符串返回curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);# 设置请求超时时间 scurl_setopt($ch, CURLOPT_TIMEOUT, 30);# 设置浏览器型号curl_setopt($ch, CURLOPT_HEADER, 'MSIE001');# 证书不检查curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);# 设置为post请求if ($ret != '') {// 开启postcurl_setopt($ch, CURLOPT_POST, 1);// post请求数据curl_setopt($ch, CURLOPT_POSTFIELDS, $ret);}// 发起请求$data = curl_exec($ch);// 有无发送异常if (curl_errno($ch) > 0) {// 把错误发送给客户端echo curl_error($ch);$data = '';}// 关闭请求curl_close($ch);return $data;}

post raw形式上传文件

public function uploadF(string $url, string $filepath){/* 使用exec函数 */$command = 'curl -F media=@' . $filepath . ' "' . $url . '"';$retval = array();exec($command, $retval, $status);$params = array();$params = json_decode($retval[0], true);if ($status != 0) {$params = array('errcode' => '-100','errmsg' => '出错',);}return $params;}
回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}