PHP 運用 array_diff() 差集,取得新增及刪除id

實在是太好用了

在開發網頁後端的時候,很常見像WordPress 這樣的分類資料,當按下儲存,表單送出後,PHP會接到的是一個POST的陣列,這就是分類id的陣列,而後端程式的處理上,新增文章的時候很好判斷,只要將送來的id都新增一筆資料就好了,但遇到文章更新的時候就相對麻煩了,因為已經有原本選擇的標籤資料了,需要去比對原本的標籤資料與表單新送過來的分類id,需要判斷哪幾個id是要新增資料,那幾個id 是要刪除資料,透過本篇介紹的array_diff() (陣列的差集),PHP原生的處裡函式就可以輕鬆篩選出來

範例:

[……]

閱讀更多

PHP 取 youtube 影片id

取得 youtube id

youtube 現在分享網址有好多種,

可能是這樣:

https://youtu.be/p_T3oNKjAT8

可能是這樣:

https://www.youtube.com/watch?v=p_T3oNKjAT8

不確定User會填入哪一種分享網址的情況下,下面這方法可以解析出這些類型的 youtube id

試試看:

<?php
/**
 * Get Youtube video ID from URL
 *
 * @param string $url
 * @return mixed Youtube video ID or FALSE if not found
 */
function getYoutubeIdFromUrl($url) {
    $parts = parse_url($url);
    if(isset($parts['query'])){
        parse_str($parts['query'], $qs);
        if(isset($qs['v'])){
            return $qs['v'];
        }else if(isset($qs['vi'])){
            return $qs['vi'];
        }
    }
    if(isset($parts['path'])){
        $path = explode('/', trim($parts['path'], '/'));
        return $path[count($path)-1];
    }
    return false;
}
// Test
$urls = array(
    'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player',
    'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player',
    'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
    'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
    'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',
    'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
    'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',
    'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player'
);
foreach($urls as $url){
    echo $url . ' : ' . getYoutubeIdFromUrl($url) . "\n";
}

 [……]

閱讀更多

Fix Google reCaptcha invalid-json error

使用 Google reCaptcha 驗證碼進行後端驗證,拋出 invalid-json 錯誤,分享解決這問題的方法:

解決方法:

開啟檢查 PHP.ini 設定,搜尋「allow_url_fopen」,若值不是 On 請改成 On 開啟它,如下:

allow_url_fopen = On

接著搜尋「extension=php_openssl.dll」,確認是否開啟,前方有分號「;」代表沒開啟

;extension=php_openssl.dll

將前方的分號「;」移除,如下:

extension=php_openssl.dll

重新啟動 SERVER

 [……]

閱讀更多

PHP 呼叫 Namespace 文件中的 function

呼叫 Namespace 文件裡的 function

/**
* https://vector.cool
*/
Namespace VECTOR\COOL;

function test() {
    echo "Hello world!\n";
}

VECTOR\COOL\test();

呼叫 Namespace 文件裡 class 中的 function

/**
* https://vector.cool
*/
namespace VECTOR\COOL;

class HELLO{
    static public function test() {
        echo "Hello world!\n";
    }
}

VECTOR\COOL\HELLO::test();

在 WordPress Action 中呼叫

<?php
/**
* https://vector.cool
*/
Namespace VECTOR\COOL;

function test() { 
	echo "Hello world!\n"; 
}
add_action('init','VECTOR\COOL\test');

參考資料:

http://php.net/manual/en/language.namespaces.nsconstants.php[……]

閱讀更多

PHP Namespace 錯誤 Warning call_user_func_array() expects parameter 1 to be a valid callback

使用 __NAMESPACE__  取得當前文件的 Namespace

呼叫 Namespace 裡的 function

/**
* https://vector.cool
*/
Namespace VECTOR\COOL;

function test() {
    echo "Hello world!\n";
}

call_user_func(__NAMESPACE__ .'\test');

呼叫 Namespace 裡的 class 中的 function

/**
* https://vector.cool
*/
Namespace VECTOR\COOL;

class Hello {
    static public function test() {
        echo "Hello world!\n";
    }
}

call_user_func(__NAMESPACE__ .'\Hello::test'); // String
call_user_func(array(__NAMESPACE__ .'\Hello', 'test')); // Array

在 WordPress Action 中呼叫

/**
* https://vector.cool
*/
Namespace V123\PLUGIN\CFU;
add_action('init',__NAMESPACE__.'\create_initial_admin_menu');

參考資料:

http://php.net/manual/en/language.namespaces.nsconstants.php
https://stackoverflow.com/questions/14682356/relative-namespaces-and-call-user-func[……]

閱讀更多

PHP print_r() 轉成字串或變數

PHP開發者應該對 print_r()  不陌生,可以直接在瀏覽器中吐出Array中的結構,在Debug時邦很大的忙,但有一種情境是 SERVER 端被動接收 API 的回傳值,可能是一個POST,但我想了解這API回傳的值是否正確,於是寫了一個Log檔去紀錄,但Array直接寫入Log,會把Array強制轉型成字串,變成 “Array” 字串,還是無法知道Array裡面傳了什麼值,print_r()其實還有第二個參數,設定第二個參數,可以把Array 輸出的文字轉成字串,這樣我們就可以很容易地在Log中清楚地看見POST回傳值了。

範例:

 
$var = print_r($arr, true);

寫在Log忠誠這樣:

 
[2018-03-11 18:34:06]tset
[2018-03-11 18:48:39]Array
(
    [CustomField1] => 
    [CustomField2] => 
    [CustomField3] => 
    [CustomField4] => 
    [MerchantID] => 2000132
    [MerchantTradeNo] => 1520765300
    [PaymentDate] => 2018/03/11 18:48:38
    [PaymentType] => WebATM_BOT
    [PaymentTypeChargeFee] => 5
    [RtnCode] => 1
    [RtnMsg] => 交易成功
    [SimulatePaid] => 0
    [StoreID] => 
    [TradeAmt] => 2000
    [TradeDate] => 2018/03/11 18:48:19
    [TradeNo] => 1803111848192948
    [CheckMacValue] => AF663A711138967263DD625549D84BF684343BEC7527A2E76759133FF93F6A87
)

 

 [……]

閱讀更多

Fix Curl Error: SSL certificate problem: unable to get local issuer certificate

最近正在安裝 WHMCS localhost 開發環境,程式安裝成功,但許可證一直無法通過,在原廠協助下,來來回回終於找到錯誤原因,就是 Curl 有錯誤,錯誤訊息:

Curl Error: SSL certificate problem: unable to get local issuer certificate

Fix Curl Error: SSL certificate problem: unable to get local issuer certificate

Curl在 WHMCS 中是很重要的,很多API的介接都需要靠它,我已經成功修復錯誤,以下分享修正方法。

修正 unable to get local issuer certificate 錯誤

Step 1. 下載CA憑證

到這個網址下載憑證: Download the certificate bundle.

Download the certificate bundle.

點紅框處下載,檔名為 cacert.pem

Step 2. 複製檔案到PHP安裝目錄中

雖然可以放在任意位置,單純是因為好找,所以我把它放在PHP安裝目錄中:

C:\php-7\extras\ssl\cacert.pem

Step 3. 確認PHP.ini啟用 mod_ssl 模組

找到 PHP.ini  檔打開它並查找 php_openssl.dll

如果它前面有分號,代表沒有開啟,請將分號移除

;extension=php_openssl.dll

如果 PHP.ini  中沒有查找到 php_openssl.dll  請於空白處,自行加入下方這一行

extension=php_openssl.dll

Step 4. 設定cacert.pem

繼續在 PHP.ini  中查找 curl.cainfo  及 openssl.cafile  這兩行

填入 Step 2 的 cacert.pem  檔案之絕對路徑,和上一個步驟一樣,前方有分號請移除分號,如果找不到這兩行請自行添加

特別注意:目錄斜線跟 Windows 檔案總管的斜線相反,要用左斜線

curl.cainfo="C:/php-7/extras/ssl/cacert.pem"
openssl.cafile="C:/php-7/extras/ssl/cacert.pem"

Step 5. 重新啟動服務器

重啟後,重新測試

Fix Curl Error: SSL certificate problem: unable to get local issuer certificate

參考:[……]

閱讀更多

綠界ECPay測試信用卡號、WebATM,模擬付款

綠界科技ECPay 網路金流

綠界科技ECPay提供多元付費方式,包含信用卡、超商付費、WebATM等,也提供物流服務,解決自助電商自動收款的難題。

綠界科技很貼心的在系統整合階段時,提供模擬環境,可以模擬付費,以便測試系統是否可以正常運行,以下提供,模擬信用卡,及WebATM資訊。

信用卡

卡號 : 4311-9522-2222-2222

有效年月 : 大於當前日期

安全碼 : 222

WebATM

選擇台新銀行進行模擬付款即可

參考:
https://www.ecpay.com.tw/CascadeFAQ/CascadeFAQ_Qa?nID=1193[……]

閱讀更多