代码拉取完成,页面将自动刷新
<?php
/**
*
* 封装curl的api请求类
*
* Class ApiLIb
*/
class ApiLIb
{
static private $requestTimeout = 3;
const REQUEST_TYPE_GET = 'GET';
const REQUEST_TYPE_POST = 'POST';
const REQUEST_TYPE_PUT = 'PUT';
const REQUEST_TYPE_DELETE = 'DELETE';
/**
* @param $apiUrl
* @param $queryString
* @param string $method
* @param string $protocol
* @return bool|string
*/
public static function sendRequest($apiUrl, $queryString, $method = 'POST', $protocol = 'Https')
{
$ch = curl_init();
if ('GET' == $method) {
curl_setopt($ch, CURLOPT_URL, (!empty($queryString) ? "$apiUrl?$queryString" : $apiUrl));
} else {
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 20); // 10 second
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// disable 100-continue
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
if ('Https' == $protocol) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
public static function request($method, $url, $data = [], $headers = [])
{
require_once "Curl.php";
$curl = new Curl();
foreach ($headers as $key => $value) {
$curl->setHeader($key, $value);
}
$curl->setOpt(CURLOPT_TIMEOUT, self::$requestTimeout);
$startTime = microtime(true);
switch ($method) {
case self::REQUEST_TYPE_GET:
$curl->get($url, $data);
break;
case self::REQUEST_TYPE_POST:
$curl->post($url, $data);
break;
case self::REQUEST_TYPE_PUT:
$curl->put($url, $data);
break;
case self::REQUEST_TYPE_DELETE:
$curl->delete($url, $data);
break;
default :
throw new \Exception();
}
// if (env('API_LOG', true)) {
// Tools::log(app()->storagePath('logs/api/' . date('Ymd')), [
// 'method' => $method,
// 'url' => $url,
// 'data' => $data,
// 'header' => $headers,
// 'code' => $curl->error_code,
// 'response' => $curl->response,
// 'time' => round((microtime(true) - $startTime), 3)
// ]);
// }
if ($curl->error_code !== 0) {
return false;
}
return $curl->response;
}
public static function getRequest($url, $data = [], $headers = [])
{
return self::request(self::REQUEST_TYPE_GET, $url, $data, $headers);
}
public static function postRequest($url, $data = [], $headers = [])
{
return self::request(self::REQUEST_TYPE_POST, $url, $data, $headers);
}
public static function putRequest($url, $data = [], $headers = [])
{
return self::request(self::REQUEST_TYPE_PUT, $url, $data, $headers);
}
public static function deleteRequest($url, $data = [], $headers = [])
{
return self::request(self::REQUEST_TYPE_DELETE, $url, $data, $headers);
}
public static function apiRequest($method, $url, $data = [], $headers = [])
{
$res = self::request($method, $url, $data, $headers);
if ($res === false) {
return false;
}
return json_decode($res, true);
}
public static function apiGetRequest($url, $data = [], $headers = [])
{
return self::apiRequest(self::REQUEST_TYPE_GET, $url, $data, $headers);
}
public static function apiPostRequest($url, $data = [], $headers = [])
{
return self::apiRequest(self::REQUEST_TYPE_POST, $url, $data, $headers);
}
public static function apiPutRequest($url, $data = [], $headers = [])
{
return self::apiRequest(self::REQUEST_TYPE_PUT, $url, $data, $headers);
}
public static function apiDeleteRequest($url, $data = [], $headers = [])
{
return self::apiRequest(self::REQUEST_TYPE_DELETE, $url, $data, $headers);
}
/**
* 根据图片路径上传图片
*
* @param $path
* @param string $api_uplpoad
* @return bool|mixed
*/
public static function uploadImageByPath($path, $api_uplpoad = "")
{
$maxwidth = "768";//设置图片的最大宽度
$maxheight = "1024";//设置图片的最大高度
$name = "/tmp/" . uniqid();//图片的名称,随便取吧
$file_type = ".jpg";//图片类型
if (!file_exists($path)) {
return false;
}
$size = filesize($path);
$image_info = getimagesize($path);
//图片缩放
if ($image_info['mime'] == 'image/jpeg') {
$im = imagecreatefromjpeg($path);
self::resizeImage($im, $maxwidth, $maxheight, $name, $file_type);//调用上面的函数
$resize_path = $name . $file_type;
$size1 = filesize($resize_path);
if (file_exists($resize_path)) {
$path = $resize_path;
}
} else if ($image_info['mime'] == 'image/png') {
$im = imagecreatefrompng($path);
self::resizeImage($im, $maxwidth, $maxheight, $name, $file_type);//调用上面的函数
$resize_path = $name . $file_type;
if (file_exists($resize_path)) {
$path = $resize_path;
}
}
$imageData = file_get_contents($path);
return self::uploadImageByData($imageData, $api_uplpoad);
}
/**
* 图片上传
*
* @param $imageData
* @param $api_uplpoad
* @return bool|mixed
*/
public static function uploadImageByData($imageData, $api_upload)
{
if (!$api_upload) {
//设置默认图片上传接口
$url = '';
} else {
$url = $api_upload;
}
$uploadResult = self::apiPostRequest($url, $imageData, ['Expect' => '']);
if (isset($uploadResult['dm_error']) && $uploadResult['dm_error'] == 0 && !empty($uploadResult['url'])) {
return $uploadResult;
} else {
return false;
}
}
/**
* 压缩图片
*
* @param $im
* @param $maxwidth
* @param $maxheight
* @param $name
* @param $filetype
*/
public static function resizeImage($im, $maxwidth, $maxheight, $name, $filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
if (($maxwidth && $pic_width > $maxwidth) && ($maxheight && $pic_height > $maxheight)) {
if ($maxwidth && $pic_width > $maxwidth) {
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}
if ($maxheight && $pic_height > $maxheight) {
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}
if ($resizewidth_tag && $resizeheight_tag) {
if ($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if ($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if ($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if (function_exists("imagecopyresampled")) {
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
} else {
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic_width, $pic_height);
}
$name = $name . $filetype;
imagejpeg($newim, $name);
imagedestroy($newim);
} else {
$name = $name . $filetype;
imagejpeg($im, $name);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。