1. 程式人生 > >PHP常用方法記錄

PHP常用方法記錄

整理收藏常用方法大全,歡迎大家補充(PHP)
1、生成32位唯一ID

/**
  * 生成唯一id[32位]
  *
  * @param string $namespace
  * @return string
  */
public static function createUniqid($namespace = ''){
	static $uniqid = '';
	$uid = uniqid("", true);
	$data = $namespace;
	$data .= isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : "";
	$data .= isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
	$data .= isset($_SERVER['LOCAL_ADDR']) ? $_SERVER['LOCAL_ADDR'] : "";
	$data .= isset($_SERVER['LOCAL_PORT']) ? $_SERVER['LOCAL_PORT'] : "";
	$data .= isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "";
	$data .= isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : "";
	$hash = strtoupper(hash('ripemd128', $uid . $uniqid . md5($data)));
	$uniqid = substr($hash,  0,  8) . substr($hash,  8,  4) . substr($hash, 12,  4) . substr($hash, 16,  4) . substr($hash, 20, 12);
	return $uniqid;
}

2、生成毫秒時間戳

 /**
   * 獲取當前時間的毫秒數
   * 
   * @return float 
   */
 public static function getMicroTime(){
	list($t1, $t2) = explode(' ', microtime());
	return (float)sprintf('%.0f'(floatval($t1)+floatval($t2))*1000);
 }

3、生成hmac_sha1加密

/**
 * 生成sig
 * 
 * @param type $data
 * @return string sig
 */
private function _generateSig($data) {

	//拼接編碼
	$paramString = base64_encode($this->_genQueryString($data));
	//key
	$key = $this->appSecret . $this->serverAuthStaticKey;
	//sha1 加密
	$sha1Key = hash_hmac("sha1", $paramString, $key, true);
	//返回sig
	return md5($sha1Key);
}

/**
 * 生成查詢字串
 * 
 * @param type $data 查詢資料
 * @return string 查詢字串
 */
private function _genQueryString($data) {
	//刪除sig
	$temp = [];
	foreach ($data as $key => $value) {
		$temp[] = $key . "=" . $value;
	}
	return implode("&", $temp);
}

4、生成唯一字串

$uniqidStr = sha1(uniqid('', TRUE) . mt_rand());

5、簡訊CURL獲取內容資訊

/**
 * PHP傳送Json物件資料, 傳送HTTP請求
 *
 * @param string $url 請求地址
 * @param array $data 傳送資料
 * @return String
 */
function http_post_json($functionName, $url, $data) {
	$ch = curl_init( $url );
	
	// application/x-www-from-urlencoded 型別
	curl_setopt( $ch, CURLOPT_POST, 1 );
	
	// 如果你想把一個頭包含在輸出中,設定這個選項為一個非零值
	curl_setopt( $ch, CURLOPT_HEADER, 0 );

	// TRUE 強制獲取一個新的連線,而不是快取中的連線
	curl_setopt( $ch, CURLOPT_FRESH_CONNECT, 1 );

	// TRUE 將curl_exec()獲取的資訊以字串返回,而不是直接輸出
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

	// TRUE 在完成互動以後強制明確的斷開連線,不能在連線池中重用
	curl_setopt( $ch, CURLOPT_FORBID_REUSE, 1 );

	// 設定一個長整形數,作為最大延續多少秒
	curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );

	// 設定 HTTP 頭欄位的陣列。格式: array('Content-type: text/plain', 'Content-length: 100')
	curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen( $data ) ) );

	// 傳遞一個作為HTTP “POST”操作的所有資料的字串
	curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );

	// 抓取URL並把它傳遞給瀏覽器
	$ret = curl_exec( $ch );
	
	echo $functionName . " : Request Info : url: " . $url . " ,send data: " . $data . "  \n";
	echo $functionName . " : Respnse Info : " . $ret . "  \n";
	
	// 關閉cURL資源,並且釋放系統資源
	curl_close( $ch );
	return $ret;
}