2016-03-27 13 views
2

以下のJSON文字列がjson_decode()に渡され、NULLが返されます。json_decode - NULLを返す

$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link); 

$homeJSON = str_replace("\xEF\xBB\xBF",'',$homeJSON); 
$homeJSON = rtrim($homeJSON); 
$homeJSON = html_entity_decode($homeJSON); 
$homeJSON = preg_replace('/\s+/', '', $homeJSON); 
$clean = rtrim($homeJSON, "\x00..\x1F"); 

$home_decoded = json_decode($clean); 

$home_decodedはまだNULLです:

{"total_goals":26,"total_games":17,"average_goals":"1.53"} 

は、ここに私のコードです。

+0

$ Home_Team_linkは何ですか? –

+0

@Abitnewそれはこの文脈で本当に重要ではありません。 JSONの例は '{" total_goals ":26、" total_games ":17、" average_goals ":" 1.53 "}' – CMS

+0

は '$ clean'のjsonですか? – Jeff

答えて

5

まず、json_decode()は、エラー状態のためにNULLを返しています。 json_last_error()を使用してエラー条件の性質を判断することができます。これは、エラーの種類を表す整数定数を返します。私はあなたが呼んでいるAPIが有効なので、返された正確に何を把握することはできません、今

<?php 
function decodeJsonError($errorCode) 
{ 
    $errors = array(
     JSON_ERROR_NONE => 'No error has occurred', 
     JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', 
     JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', 
     JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', 
     JSON_ERROR_SYNTAX => 'Syntax error', 
     JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', 
     JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded', 
     JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', 
     JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given' 
    ); 

    if (isset($errors[$errorCode])) 
    { 
     return $errors[$errorCode]; 
    } 

    return 'Unknown error'; 
} 

:あなたは、これらが(json_last_error()のマニュアルページで説明エラーコードから構築された)この機能を使用してデコードすることができますリンクが提供されていなかった(と私は、これが機密データであってもよい理解)、しかし、のは、別のアプローチを試してみましょう、とだけのみJSONは何からAPIが戻っているを抽出しよう:

<?php 

$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link); 

if (!preg_match('/(\{"[^\}]+\})/', $homeJSON, $matches)) 
{ 
    echo "An error occurred; no JSON found."; 
    return; 
} 

$home_decoded = json_decode($matches[1]); 

これは、私のためにAPIの実際の出力(適切なURLなし)に対処する:

file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com"); 

API自体がJSONと組み合わされたHTMLを返していますが、

編集:あなたは、有効なURLを提供しているので、私はいくつかのより多くのデバッグをしました:

php > print_r(array_map('dechex', array_map('ord', str_split($homeJSON)))); 
Array 
(
    [0] => ef 
    [1] => bb 
    [2] => bf 
    [3] => 7b 
    [4] => 22 
    [5] => 74 
    [6] => 6f 
    [7] => 74 
    [8] => 61 
    [9] => 6c 
    ... 

バイト7bは有効なJSONが始まるオープニング{です。バイト0-3は、UTF-8 Byte Order Markです。だから、上記の私の正規表現のソリューションがまだ動作しますが、我々はまた、ちょうどBOMと、このような任意の浮遊ヌル文字をクリーンアップすることができます

$homeJSON = file_get_contents("http://strategy-bets.com/archive/archive.php?baseurl=http://www.totalcorner.com".$Home_Team_Link); 

$homeJSON = trim($homeJSON, "\x0\xEF\xBB\xBF"); 

$home_decoded = json_decode($homeJSON); 

これは私を与える:

php > var_dump(json_decode($jsonCopy)); 
object(stdClass)#1 (3) { 
    ["total_goals"]=> 
    int(26) 
    ["total_games"]=> 
    int(17) 
    ["average_goals"]=> 
    string(4) "1.53" 
}