我很高興有類似的問(wèn)題,但我無(wú)法獲得任何與我的(繼承的)程式碼一起使用的解決方案,因此將不勝感激您可以提供的任何幫助。
我們透過(guò)表單整合使用 Sage Pay / Opayo 支付網(wǎng)關(guān)。
完成/失敗後,客戶將被重定向到我們網(wǎng)站上帶有加密 _GET 字串的 URL。
解密後,呼叫 getToken 函數(shù) ( $values = getToken($Decoded);
) 從陣列中取得值。
不過(guò),並非所有標(biāo)記都始終被填充,我懷疑這些空值可能是問(wèn)題的根源。
程式碼在 PHP 7.1 上運(yùn)作良好,但在 PHP 8.1 上會(huì)拋出例外:
[2023 年 5 月 3 日 15:12:15 歐洲/倫敦] PHP 致命錯(cuò)誤:未捕獲錯(cuò)誤:嘗試在 /home/sitename/public_html/ch_functions.php:166 中的 null 上分配屬性“start” 堆疊追蹤: #0 /home/sitename/public_html/not_completed.php(32): getToken('VendorTxCode=AP...') #1 {主要} 拋出在 /home/sitename/public_html/ch_functions.php 第 166 行
程式碼是,在 $resultArray[$i]->start = $start;
上失敗的是:
function getToken($thisString) { // List the possible tokens $Tokens = array("Status","StatusDetail","VendorTxCode","VPSTxId","TxAuthNo","Amount","AVSCV2","AddressResult","PostCodeResult","CV2Result","GiftAid","3DSecureStatus","CAVV", "AddressStatus", "PayerStatus", "CardType", "Last4Digits","BankAuthCode","DeclineCode"); // Initialise arrays $output = array(); $resultArray = array(); // Get the next token in the sequence for ($i = count($Tokens)-1; $i >= 0 ; $i--){ // Find the position in the string $start = strpos($thisString, $Tokens[$i]); // If it's present if ($start !== false){ // Record position and token name $resultArray[$i]->start = $start; $resultArray[$i]->token = $Tokens[$i]; } } // Sort in order of position sort($resultArray); // Go through the result array, getting the token values for ($i = 0; $i<count($resultArray); $i++){ // Get the start point of the value $valueStart = $resultArray[$i]->start + strlen($resultArray[$i]->token) + 1; // Get the length of the value if ($i==(count($resultArray)-1)) { $output[$resultArray[$i]->token] = substr($thisString, $valueStart); } else { $valueLength = $resultArray[$i+1]->start - $resultArray[$i]->start - strlen($resultArray[$i]->token) - 2; $output[$resultArray[$i]->token] = substr($thisString, $valueStart, $valueLength); } } // Return the ouput array return $output; }
您將 $resultArray[$i]
作為物件引用,但該陣列為空,因此沒有可供引用的物件。在 PHP 7.4 及更早版本中,您可以執(zhí)行以下操作:
$x = []; $x[0]->foo = 1;
PHP 會(huì)在 $x[0]
處動(dòng)態(tài)建立一個(gè) stdClass
對(duì)象,然後動(dòng)態(tài)建立 foo 屬性,但會(huì)發(fā)出警告:
您目前正在抑製或忽略此警告。在 PHP 8.0 中,這現(xiàn)在會(huì)產(chǎn)生致命錯(cuò)誤。因此,只需在嘗試設(shè)定其值之前建立空物件即可:
if ($start !== false) { $resultArray[$i] = new stdClass(); $resultArray[$i]->start = $start; $resultArray[$i]->token = $Tokens[$i]; }
或:
if ($start !== false) { $resultArray[$i] = (object) [ 'start' => $start, 'token' => $Tokens[$i], ]; }