You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Helper/Baofu/HttpClient.php

110 lines
4.3 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Helper\Baofu;
use Exception;
class HttpClient {
/**
*
* @param array $data
* @param string $url
* @param string $dataType
* @return string
*/
public static function post($url, $data, $dataType = 'form')
{
$postData = $data;
$curl = curl_init(); // 启动一个CURL会话
$postDataString = '';
if($dataType == 'json'){
$postDataString = json_encode($postData);;//格式化参数
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postDataString)));
} else {
$postDataString = http_build_query($postData);//格式化参数
echo $postDataString;
}
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); //从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_SSLVERSION, 6); //CURL_SSLVERSION_DEFAULT (0), CURL_SSLVERSION_TLSv1 (1), CURL_SSLVERSION_SSLv2 (2), CURL_SSLVERSION_SSLv3 (3), CURL_SSLVERSION_TLSv1_0 (4), CURL_SSLVERSION_TLSv1_1 (5) CURL_SSLVERSION_TLSv1_2 (6) 中的其中一个。
curl_setopt($curl, CURLOPT_POST, true); //发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $postDataString); //Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 60); //设置超时限制防止死循环返回
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
$tmpInfo = curl_error($curl);//捕抓异常
}
curl_close($curl); //关闭CURL会话
return $tmpInfo; //返回数据
}
/*
*get请求 urldecode数据传输(支持http和https)
*@param $url接口地址 string
*@param $headers请求报文头body数据 array
*@return type
* */
public static function get($url, $headers = array())
{
$ch = curl_init(); //初始化
if (strpos($url, 'https') === 0)
{
//当请求https的数据时,会要求证书,这时候,加上下面这两个参数,规避ssl的证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //https请求 不验证证书和hosts
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}
if (!empty($headers))
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置http头部
}
curl_setopt($ch, CURLOPT_URL, $url); //设置获取的url地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); //连接超时(秒)
curl_setopt($ch, CURLOPT_TIMEOUT, 4); //执行超时(秒)
$outPut = curl_exec($ch); //执行命令,并获取结果
curl_close($ch); //关闭curl资源,释放系统资源
return $outPut;
}
/**
* 页面接口
* @param string $url
* @param array $data
* @return string
*/
public static function createForwardHtml($url, $data, $isAutoSubmit = true)
{
if(empty($data)){
throw new Exception('[data]参数不能为空!');
}
if(empty($url)){
throw new Exception('[url]参数不能为空!');
}
if(!is_array($data)){
throw new Exception('[data]参数不是数组!');
}
$html = '<html>';
$inputType = 'hidden';
if ($isAutoSubmit) {
$html .= '<body onLoad="document.actform.submit()">正在处理请稍候............................';
} else {
$inputType = 'text';
$html .= '<body>';
}
$html .= '<form id="actform" name="actform" method="post" action="'.$url.'">';
foreach($data as $feild => $value){
$html .= '<input type="' . $inputType . '" name="' . $feild . '" value="' . $value . '">';
}
$html .= '</form></body></html>';
return $html;
}
}