博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP对CURL函数的封装,支持GET/POST请求
阅读量:6911 次
发布时间:2019-06-27

本文共 2126 字,大约阅读时间需要 7 分钟。

hot3.png

代码如下

/** * curl 函数 * @param string $url 请求的地址 * @param string $type POST/GET/post/get * @param array $data 要传输的数据 * @param string $err_msg 可选的错误信息(引用传递) * @param int $timeout 超时时间 * @param array 证书信息 * @author 勾国印 */function GoCurl($url, $type, $data = false, &$err_msg = null, $timeout = 20, $cert_info = array()){    $type = strtoupper($type);    if ($type == 'GET' && is_array($data)) {        $data = http_build_query($data);    }    $option = array();    if ( $type == 'POST' ) {        $option[CURLOPT_POST] = 1;    }    if ($data) {        if ($type == 'POST') {            $option[CURLOPT_POSTFIELDS] = $data;        } elseif ($type == 'GET') {            $url = strpos($url, '?') !== false ? $url.'&'.$data :  $url.'?'.$data;        }    }    $option[CURLOPT_URL]            = $url;    $option[CURLOPT_FOLLOWLOCATION] = TRUE;    $option[CURLOPT_MAXREDIRS]      = 4;    $option[CURLOPT_RETURNTRANSFER] = TRUE;    $option[CURLOPT_TIMEOUT]        = $timeout;    //设置证书信息    if(!empty($cert_info) && !empty($cert_info['cert_file'])) {        $option[CURLOPT_SSLCERT]       = $cert_info['cert_file'];        $option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass'];        $option[CURLOPT_SSLCERTTYPE]   = $cert_info['cert_type'];    }    //设置CA    if(!empty($cert_info['ca_file'])) {        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO        $option[CURLOPT_SSL_VERIFYPEER] = 1;        $option[CURLOPT_CAINFO] = $cert_info['ca_file'];    } else {        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO        $option[CURLOPT_SSL_VERIFYPEER] = 0;    }    $ch = curl_init();    curl_setopt_array($ch, $option);    $response = curl_exec($ch);    $curl_no  = curl_errno($ch);    $curl_err = curl_error($ch);    curl_close($ch);    // error_log    if($curl_no > 0) {        if($err_msg !== null) {            $err_msg = '('.$curl_no.')'.$curl_err;        }    }    return $response;}

使用方法如下:

$url   = '请求地址';$data = array(            'phoneNum' => '18614064456',        );$json = GoCurl($url, $data, 'POST', $error_msg);$array = json_decode($json, true);print_r($array);

转载于:https://my.oschina.net/botkenni/blog/831546

你可能感兴趣的文章
oracle的后门网站下载资源
查看>>
多国科学家共议泛第三极环境与“一带一路”
查看>>
我的友情链接
查看>>
Hive Server对SQL语句的编译过程 : 前奏
查看>>
zabbix3.2.6.1升级3.4.4图文心得
查看>>
SNMP在企业网中的应用
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
64,管道符,控制命令,变量
查看>>
Java自带的性能监测工具之jmap
查看>>
2-openstack基础环境准备
查看>>
SVN安装采用AD认证
查看>>
hive常用语法示例
查看>>
unicode分类
查看>>
hadoop-003-源码之编译eclipse-plugin
查看>>
即使对象内容都为空,对象本身也是非空的
查看>>
MySQL事务隔离级别详解
查看>>
storm记录--3--Storm的基本概念
查看>>
×××灯marquee标签应用详解
查看>>
彻底弄懂css中单位px和em,rem的区别
查看>>