2016-04-01 26 views
3

最近PHP 7.0.4とnginx 1.8.1にアップグレードしましたが、私のアプリケーションはZend Framework(Magento 1.9.2.1)を使用しています。そのアップグレード以来、当社の顧客は時々、「復号化が失敗しました:構文エラー」を取得PHP7、JSON、Zend(デコードに失敗しました:構文エラー)

public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY) 
{ 
    $encodedValue = (string) $encodedValue; 
    if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) { 
     $decode = json_decode($encodedValue, $objectDecodeType); 

     // php < 5.3 
     if (!function_exists('json_last_error')) { 
      if (strtolower($encodedValue) === 'null') { 
       return null; 
      } elseif ($decode === null) { 
       #require_once 'Zend/Json/Exception.php'; 
       throw new Zend_Json_Exception('Decoding failed'); 
      } 
     // php >= 5.3 
     } elseif (($jsonLastErr = json_last_error()) != JSON_ERROR_NONE) { 
      #require_once 'Zend/Json/Exception.php'; 
      switch ($jsonLastErr) { 
       case JSON_ERROR_DEPTH: 
        throw new Zend_Json_Exception('Decoding failed: Maximum stack depth exceeded'); 
       case JSON_ERROR_CTRL_CHAR: 
        throw new Zend_Json_Exception('Decoding failed: Unexpected control character found'); 
       case JSON_ERROR_SYNTAX: 
        throw new Zend_Json_Exception('Decoding failed: Syntax error'); 
       default: 
        throw new Zend_Json_Exception('Decoding failed'); 
      } 
     } 

     return $decode; 
    } 

にスローされordermを提出するとき、私は空の文字列をエンコードする際にPHP7とJSONデコードが異なった動作をバグについて読みました。このバグがPHP7や私のアプリケーション/サーバーに関係しているかどうかは誰にも分かりますか?

おかげ

答えて

8

これはjson_decodeの絶対に正常な動作です。

指定された文字列が有効なJSON文字列でない場合、この例外がスローされます。

すでに述べたように、空の文字列も有効なJSON文字列ではありません。

json_decode('Hello') // invalid 
json_decode("Hello") //invalid 

しかし:

json_decode("'Hello'") // valid JSON string 

空の文字列がPHP7ので、例外が発生します! 「文字列にキャストした後、JSONの構文エラーで空の文字列(NULL、FALSE)の結果であることを、空のPHPの文字列または値に等しい第一引数でjson_decodeを呼び出す。」

あなたの質問に答えてください:私の意見では、PHPバージョンのダウングレードがオプションではない場合、あなたのアプリケーションには解決しなければならない問題があります。

Magenta関数は、json_decode関数に渡す前に、JSON文字列の有効な表現をチェックする必要があります。

変数が文字列(is_string)でないか、または空の場合は、変数を{}に設定してください。

これが問題でない場合は、文字列がエンコードされていない可能性があります。 json_decodeに渡された文字列を何回もエンコードすると、その例外も発生します。

0

Zendのデコーダのビルドをtrueに設定するのが一つの可能​​性がありますが、これはずっと遅いかもしれませんが、デフォルトパッケージのエラーが出るPHP7で動作します。

(クラスZend_Json)は、$useBuiltinEncoderDecoderをtrueに設定します。

public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY) 
{ 
    $encodedValue = (string) $encodedValue; 
    if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) { 

うまく動作しているようだreturn Zend_Json_Decoder::decode($encodedValue, $objectDecodeType);が使用されますこの方法です。

0

ダーティハック:私の場合は

それは空のオブジェクトがエラーを投げるでしょうPHP7とMagentoの2.2で問題でした。これにより、製品インデクサーが完了できなくなります。

このケースでは、空の配列戻り値(ファイルvendor/magento/zendframework1/library/Zend/Json)を追加しました。php):

public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY) 
    { 
     $encodedValue = (string) $encodedValue; 

     if($encodedValue == "a:0:{}") { return []; } //<- added: return an empty array 

     if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) { 
.... 
0

私は同じ問題があります。私は、より良い解決策を待って、次のような一時的な修正があります

//return Zend_Json::decode($data->params); 
    return json_decode($data->params); 
関連する問題