74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2025/3/22} {14:06}
|
||
*/
|
||
|
||
namespace app\api\logic;
|
||
|
||
|
||
class BaseLogic
|
||
{
|
||
|
||
static function curl_post_json(string $url, array $postdata, $header=[]) {
|
||
$tokenArr = (new TokenLogic())->getToken();
|
||
$postdata['appId'] = $tokenArr['appId'];
|
||
$domain = config('gewechat.base_url');
|
||
$timeout = 4;
|
||
$connect_timeout = 1;
|
||
$set_time_limit = 5;
|
||
if($timeout + $connect_timeout < $set_time_limit) throw new \Exception('脚本超时值必须大于等于连接超时与请求处理超时之和');
|
||
set_time_limit($set_time_limit);
|
||
$header = array_merge($header, array(
|
||
'X-GEWE-TOKEN:' .$tokenArr['token'],
|
||
'Content-Type: application/json',
|
||
'Accept: application/json'
|
||
));
|
||
|
||
//初始化
|
||
$curl = curl_init();
|
||
//设置抓取的url
|
||
curl_setopt($curl, CURLOPT_URL, $domain.$url);
|
||
//设置头文件的信息作为数据流输出
|
||
curl_setopt($curl, CURLOPT_HEADER, 0);
|
||
//设置获取的信息以文件流的形式返回,而不是直接输出。
|
||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||
// 超时设置
|
||
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
|
||
//发起连接前等待的时间,如果设置为0,则无限等待。
|
||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
|
||
|
||
// 超时设置,以毫秒为单位
|
||
// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
|
||
|
||
// 设置请求头
|
||
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
||
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
|
||
|
||
//设置post方式提交
|
||
curl_setopt($curl, CURLOPT_POST, 1);
|
||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postdata,JSON_UNESCAPED_UNICODE));
|
||
//执行命令
|
||
$data = curl_exec($curl);
|
||
|
||
// 显示错误信息
|
||
if (curl_error($curl)) {
|
||
throw new \Exception('cURL error: ' . curl_error($curl));
|
||
//返回错误码
|
||
//return ['code'=>curl_errno($curl), 'msg'=>curl_error($curl)];
|
||
} else {
|
||
//关闭句柄
|
||
curl_close($curl);
|
||
// 返回的内容
|
||
$res = json_decode($data, true);
|
||
if($res['ret'] != 200){
|
||
throw new \Exception($res['msg']);
|
||
}
|
||
return $res;
|
||
}
|
||
}
|
||
} |