PHP 小功能

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
 
/**
 * @link http://kodcloud.com/
 * @author warlee | e-mail:[email protected]
 * @copyright warlee 2014.(Shanghai)Co.,Ltd
 * @license http://kodcloud.com/tools/license/license.txt
 */
 
 
/**
 * 安全讀取文件,避免併發下讀取數據爲空
 * 
 * @param $file 要讀取的文件路徑
 * @param $timeout 讀取超時時間 
 * @return 讀取到的文件內容 | false - 讀取失敗
 */
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);     // 隨機等待1~50ms再試
        }
    }
    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;
    }
}
 
/**
 * 安全寫文件,避免併發下寫入數據爲空
 * 
 * @param $file 要寫入的文件路徑
 * @param $buffer 要寫入的文件二進制流(文件內容)
 * @param $timeout 寫入超時時間 
 * @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);   // 隨機等待1~50ms再試
        }
    }
    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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章