2020-03-11 14:23:25 +08:00
|
|
|
|
<?php
|
2022-08-16 15:21:08 +08:00
|
|
|
|
/*
|
|
|
|
|
* @Author: TaoLer <317927823@qq.com>
|
|
|
|
|
* @Date: 2021-12-06 16:04:50
|
|
|
|
|
* @LastEditTime: 2022-08-01 10:09:11
|
|
|
|
|
* @LastEditors: TaoLer
|
|
|
|
|
* @Description: 优化版
|
|
|
|
|
* @FilePath: \github\TaoLer\extend\taoler\com\Api.php
|
|
|
|
|
* Copyright (c) 2020~2022 https://www.aieok.com All rights reserved.
|
|
|
|
|
*/
|
2020-03-11 14:23:25 +08:00
|
|
|
|
|
|
|
|
|
namespace taoler\com;
|
2022-11-18 10:31:44 +08:00
|
|
|
|
use think\Response;
|
2020-03-11 14:23:25 +08:00
|
|
|
|
|
|
|
|
|
class Api
|
|
|
|
|
{
|
2023-07-03 12:52:34 +08:00
|
|
|
|
public $code;
|
2022-11-20 21:56:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param $url
|
|
|
|
|
* @param $data
|
|
|
|
|
* @return mixed|\think\response\Json
|
|
|
|
|
*/
|
2020-03-18 21:30:30 +08:00
|
|
|
|
public static function urlPost($url, $data)
|
2020-03-11 14:23:25 +08:00
|
|
|
|
{
|
|
|
|
|
if($url == ''){
|
|
|
|
|
return json(['code'=>-1,'msg'=>'800']);
|
|
|
|
|
}
|
|
|
|
|
$ch =curl_init ();
|
|
|
|
|
curl_setopt($ch,CURLOPT_URL, $url);
|
|
|
|
|
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
|
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
|
|
|
|
|
curl_setopt($ch,CURLOPT_POST, 1);
|
2021-03-19 14:38:06 +08:00
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
|
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
|
2021-07-22 10:54:03 +08:00
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置超时限制 防止死循环
|
2021-03-19 14:38:06 +08:00
|
|
|
|
|
2021-03-18 16:21:27 +08:00
|
|
|
|
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
|
2021-01-30 22:25:11 +08:00
|
|
|
|
$res = curl_exec($ch);
|
2020-03-11 14:23:25 +08:00
|
|
|
|
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
|
|
|
|
curl_close($ch);
|
2022-11-26 20:32:21 +08:00
|
|
|
|
if($httpCode == 200){
|
2021-01-30 22:25:11 +08:00
|
|
|
|
return json_decode($res);
|
2020-03-11 14:23:25 +08:00
|
|
|
|
} else {
|
2021-03-11 15:49:28 +08:00
|
|
|
|
return json_decode('{"code":-1,"msg":"远程服务器失败,稍后重试"}'); //转换为对象
|
2020-03-11 14:23:25 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 21:30:30 +08:00
|
|
|
|
|
|
|
|
|
public static function urlGet($url)
|
|
|
|
|
{
|
|
|
|
|
$ch =curl_init ();
|
|
|
|
|
curl_setopt($ch,CURLOPT_URL, $url);
|
2021-03-11 15:49:28 +08:00
|
|
|
|
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。 1表示传输数据,为0表示直接输出显示。
|
|
|
|
|
//curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
|
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); //启用时会将头文件的信息作为数据流输出。 参数为1表示输出信息头,为0表示不输出
|
2020-03-18 21:30:30 +08:00
|
|
|
|
$data = curl_exec($ch);
|
|
|
|
|
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
|
|
|
|
curl_close($ch);
|
2022-11-26 20:32:21 +08:00
|
|
|
|
if($httpCode == 200){
|
2020-03-18 21:30:30 +08:00
|
|
|
|
return json_decode($data);
|
|
|
|
|
} else {
|
2021-03-11 15:49:28 +08:00
|
|
|
|
//$status ='{"code":-1,"msg":"远程服务器失败"}'; //字符串
|
|
|
|
|
return json_decode('{"code":-1,"msg":"远程服务器失败,稍后重试"}'); //转换为对象
|
2020-03-18 21:30:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-16 15:21:08 +08:00
|
|
|
|
|
|
|
|
|
public static function urlGetRespond($url)
|
|
|
|
|
{
|
|
|
|
|
$ch =curl_init ();
|
|
|
|
|
curl_setopt($ch,CURLOPT_URL, $url);
|
|
|
|
|
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。 1表示传输数据,为0表示直接输出显示。
|
|
|
|
|
//curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 20);
|
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); //启用时会将头文件的信息作为数据流输出。 参数为1表示输出信息头,为0表示不输出
|
|
|
|
|
$data = curl_exec($ch);
|
|
|
|
|
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
|
|
|
|
curl_close($ch);
|
2022-11-26 20:32:21 +08:00
|
|
|
|
if($httpCode == 200){
|
2022-08-16 15:21:08 +08:00
|
|
|
|
//return json_decode($data);
|
|
|
|
|
return json(['code'=>0, 'data'=>$data]);
|
|
|
|
|
} else {
|
|
|
|
|
//return json_decode('{"code":-1,"msg":"远程服务器失败,稍后重试"}'); //转换为对象
|
|
|
|
|
return json(['code'=>-1,'msg'=>'Remote server failed, try again later']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 21:30:30 +08:00
|
|
|
|
|
|
|
|
|
public static function get_real_ip()
|
|
|
|
|
{
|
|
|
|
|
static $realip;
|
|
|
|
|
if (isset($_SERVER)) {
|
|
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
|
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
|
|
} else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
|
|
|
|
|
$realip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
|
|
} else {
|
|
|
|
|
$realip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (getenv('HTTP_X_FORWARDED_FOR')) {
|
|
|
|
|
$realip = getenv('HTTP_X_FORWARDED_FOR');
|
|
|
|
|
} else if (getenv('HTTP_CLIENT_IP')) {
|
|
|
|
|
$realip = getenv('HTTP_CLIENT_IP');
|
|
|
|
|
} else {
|
|
|
|
|
$realip = getenv('REMOTE_ADDR');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $realip;
|
|
|
|
|
}
|
2020-03-11 14:23:25 +08:00
|
|
|
|
}
|