發表文章

目前顯示的是有「PHP」標籤的文章

Agent

User Agent 解析工具,是比較早期使用的套件,當時主要用來判斷目前裝置是電腦還是手機,以便依據不同的裝置顯示不同的內容。

Intervention Image

[Sublime 4] CodeFormatter 套件不支援 PHP8 問題

圖片
我把自己電腦 PHP 版本升級到 8.2,CodeFormatter 就壞了,噴出 Call to undefined function each() error。

PHP AES 加密筆記

主要重點為: hex2bin 函數 openssl_encrypt 已經是 PKCS7 padding $key = hex2bin($key); $iv = hex2bin($iv); $data = hex2bin($data); $data = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv); 參考文件 AES 256 encryption PHP with Padding

PHP 判斷寫法

一般寫法 if ($a) { $a = $a; } else { $a = $b; } 簡寫 $a = $a ? $a : $b; PHP 5.3 以後可以寫的更簡單一點 $a = $a ?: $b; PHP 7.0 開始支援兩個問號 ( ?? ),兩個問號是什麼意思? $a = $a ?? $b; 兩個問號不是單純的 if 判斷,他是 isset,等同於: $a = isset($a) ? $a : $b;

cURL error 60: Peer's Certificate issuer is not recognized.

我們公司真的很常遇到 curl 問題,這次是要去爬別人網站的資料遇到 cURL error 60: Peer's Certificate issuer is not recognized.

[PHP] 防止 json_encode 把中文轉成 Unicode

因為串接其他平台,我會把其他平台回傳的一部份資料 json_encode 之後存進資料庫,可是最近要用 phpMyAdmin 進行查詢時發現中文都被轉成 Unicode,造成查詢上的困難,而且也沒辦法直觀的看到資料內容。

[PHP] 如何去除 0xA0

直接說結論,用 trim() 可以去掉,寫法如下: $string = trim($string, chr(0xC2) . chr(0xA0)); 而且一定要寫 chr(0xC2),如果只寫 chr(0xA0) 沒有作用 // 這樣不行 $string = trim($string, chr(0xA0)); 參考文件 PHP: trim - Manual Delete spaces php

[PHP] json_decode return null

最近串某平台的帳號,對方 return 回來的資料是 josn 格式,可是我用 json_decode() 卻解不開,回傳 null。

cURL error 60: Issuer certificate is invalid.

圖片
最近在兩台 Server 之間 call api 會出現 cURL error 60: Issuer certificate is invalid. 的問題,詢問公司 MIS,他說是 Server 憑證問題,要在 Server 執行指令 yum update ca-certificates 更新套件。其實我也不是很懂,就先記錄下來吧~

[PHP] file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:

繼上次 file_get_contents 遇到沒有指定 user-agent 就不能撈資料,這次又遇到新問題啦! 這一次的錯誤訊息是: file_get_contents():SSL operation failed with code 1. OpenSSL Error messages: 主要原因就是網站 SSL 問題,撇除 Server 設定問題,程式工程師可以做的部分就是直接把它換成 curl,沒錯...又是換成 curl。 原來的程式碼: $contents = file_get_contents($url, false, $context); 改版後的程式碼: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $sendInfo); curl_setopt($ch, CURLOPT_SSLVERSION, 6); $contents = curl_exec($ch); curl_close($ch); 這一次我遇到的是 SSL 版本問題,所以這段程式的重點在於 curl_setopt($ch, CURLOPT_SSLVERSION, 6),其中 CURLOPT_SSLVERSION 有多個選項分別代表不同的版號,可以參考 https://www.php.net/manual/en/function.curl-setopt.php ,而且不知道為什麼我寫常數 CURL_SSLVERSION_TLSv1_2 還不行,一定要寫 6。

CentOS 7 安裝 PHP zip 套件

下載安裝 yum install zip unzip php-pecl-zip 查看目前載入的 ini php --ini 查看目前有裝的套件 php -m

[PHP] Call to undefined function stats_absolute_deviation()

圖片
PHP 預設沒有安裝統計套件,如果要用到需要自行安裝,參考文件 https://www.php.net/manual/en/stats.installation.php ,裡面有提供套件 網址 。

[PHP] file_get_contents: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

之前寫了一個小程式用 file_get_contents 去爬別人家網站的資料,但最近突然無法使用,噴出錯誤訊息: Warning : file_get_contents(<url>): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in <Your Program> on line <Error Line> 經查詢是因為沒有指定 user-agent,這樣一來就沒辦法簡單用 file_get_contents 去撈資料,只好改成 curl,程式碼分別如下: 原來的程式碼: $contents = file_get_contents($url); 改版後的程式碼: $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $url); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'); curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); $contents = curl_exec($curl_handle); curl_close($curl_handle);

什麼是執行緒安全?安裝 PHP 時怎麼選?

這個資訊網路上很多,我盡量用自己的話描述,因為是寫給自己看的。

[Windows] 更新 PHP 到版本 7.0

目標是把既有的 PHP 升級,升級前後的的環境如下: 原來的環境 Apache 2.4 (VC11) php 5.6 更新後的環境 Apache 2.4 (VC14) php 7.0

[PHP] Class 'ZipArchive' not found

在 Windows下的解決辦法是: 在 php.ini 中,將 extension=php_zip.dll 前面的分號 “;” 刪除,同樣在 php.ini 中,將 zlib.output_compression = Off 改為 zlib.output_compression = On 重啟 Apache