123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /**
- * Request.php UTF-8
- * 各个请求函数
- *
- * @date : 2016年11月16日下午2:45:36
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 7.0
- * @modified: 2016年11月16日下午2:45:36
- */
- /**
- * Rsaauth.class.php UTF-8
- * 对称 非对称加密 请求与返回
- *
- * @date : 2016年11月9日下午11:46:45
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 7.0
- * @modified: 2016年11月9日下午11:46:45
- */
- namespace huo\controller\request;
- use huo\controller\common\Base;
- use think\Log;
- class Request extends Base {
- /**
- * CP 回调请求函数
- *
- * @param $url string 请求地址与端口
- * @param $params string post数据
- *
- * @return int 请求结果
- */
- public static function cpPayback($url, $params) {
- $curl = curl_init(); //初始化curl
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, 0); // 过滤HTTP头
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
- curl_setopt($curl, CURLOPT_POST, 1); // post传输数据
- curl_setopt($curl, CURLOPT_POSTFIELDS, $params);// post传输数据
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
- //https 请求
- if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- $header = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- //https 请求
- if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- $responseText = curl_exec($curl);
- curl_close($curl);
- $rs = strtoupper($responseText);
- if ('SUCCESS' == $rs) {
- $result = 1;
- } else {
- $result = 0;
- }
- return $result;
- }
- /**
- * CP 回调请求函数
- *
- * @param string $url 请求地址与端口
- * @param array $params post数据
- *
- * @return mixed|false 请求失败为false 其他为请求结果
- */
- public static function httpJsonpost($url, $params) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置等待时间
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
- //https 请求
- if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- curl_setopt(
- $ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: '.strlen($params))
- );
- $_rc = curl_exec($ch);
- $_err_no = curl_errno($ch);
- if (0 != $_err_no) {
- Log::write(array($url, $params, $_err_no, $_rc), Log::ERROR);
- return false;
- } else {
- return $_rc;
- }
- }
- /**
- * @param string $url
- * @param bool $params
- * @param int $is_post
- *
- * @param array $header
- *
- * @return bool|mixed
- */
- public static function http($url, $params = false, $is_post = 0, $header = []) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- if (empty($header)) {
- $header = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- //https 请求
- if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- if ($is_post) {
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
- curl_setopt($ch, CURLOPT_URL, $url);
- } else {
- if ($params) {
- curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
- } else {
- curl_setopt($ch, CURLOPT_URL, $url);
- }
- }
- $response = curl_exec($ch);
- if ($response === false) {
- return false;
- }
- curl_close($ch);
- return $response;
- }
- }
|