1. 實現短連接的還原
function restoreUrl($shortUrl) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $shortUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0');
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
$data = curl_exec($curl);
$curlInfo = curl_getinfo($curl);
curl_close($curl);
if($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
return $curlInfo['redirect_url'];
}
return '';
}
$shortUrl = 'https://m.tb.cn/h.ew5NAEA';
$orinalUrl = restoreUrl($shortUrl);
if($orinalUrl) {
echo "短網址 {$shortUrl} 的還原結果:{$orinalUrl}";
} else {
echo "短網址還原失敗";
}
2. 隨機一張圖
$img_array = glob('image/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);
if(count($img_array) == 0) die('沒找到圖片文件。請先上傳一些圖片到 '.dirname(__FILE__).'\image');
header('Content-Type: image/png');
$content = $img_array[array_rand($img_array)];
echo((file_get_contents($content)));
3. 隨機一句話
$filename = 'data.dat';
header('Content-type: text/html; charset=utf-8');
if(!file_exists($filename)) {
die($filename . ' 數據文件不存在');
}
$data = array();
$fh = fopen($filename, 'r');
while (!feof($fh)) {
$data[] = fgets($fh);
}
fclose($fh);
$result = $data[array_rand($data)];
echo $result;
$data = file_get_contents($filename);
$data = explode(PHP_EOL, $data);
$result = $data[array_rand($data)];
$result = str_replace(array("\r","\n","\r\n"), '', $result);
echo $result;
4. 併發下讀寫文件
<?php
function file_read_safe($file, $timeout = 5) {
if (!$file || !file_exists($file)) return false;
$fp = @fopen($file, 'r');
if (!$fp) return false;
$startTime = microtime(true);
do {
$locked = flock($fp, LOCK_EX | LOCK_NB);
if (!$locked) {
usleep(mt_rand(1, 50) * 1000);
}
}
while ((!$locked) && ((microtime(true) - $startTime) < $timeout));
if ($locked && filesize($file) >= 0) {
$result = @fread($fp, filesize($file));
flock($fp, LOCK_UN);
fclose($fp);
if (filesize($file) == 0) {
return '';
}
return $result;
} else {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
}
function file_write_safe($file, $buffer, $timeout = 5) {
clearstatcache();
if (strlen($file) == 0 || !$file) return false;
if (!file_exists($file)) {
@file_put_contents($file, '');
}
if(!is_writeable($file)) return false;
$fp = fopen($file, 'r+');
$startTime = microtime(true);
do {
$locked = flock($fp, LOCK_EX);
if (!$locked) {
usleep(mt_rand(1, 50) * 1000);
}
}
while ((!$locked) && ((microtime(true) - $startTime) < $timeout));
if ($locked) {
$tempFile = $file.'.temp';
$result = file_put_contents($tempFile, $buffer, LOCK_EX);
if (!$result || !file_exists($tempFile)) {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
@unlink($tempFile);
ftruncate($fp, 0);
rewind($fp);
$result = fwrite($fp, $buffer);
flock($fp, LOCK_UN);
fclose($fp);
clearstatcache();
return $result;
} else {
flock($fp, LOCK_UN);
fclose($fp);
return false;
}
}
轉載: https://mkblog.cn/