ugrás a tartalomhoz

json_decode probléma

DarkHcK · 2014. Feb. 25. (K), 09.45
Sziasztok!
Szeretném a segítségeteket kérni, mert elakadtam.

Van egy ilyenem:
$rawData = file_get_contents('https://www.valami.hu/api/coupon/create', false, $context);

Dumpolva ezt adja:

{"code":"XXXXXX","link":"https:\/\/www.valami.hu\/api\/coupon\/link?state=fe12ca00efd84772d83bd4354d39305d"}


Majd ezt próbálom meg decode -olni, de NULL a visszatérési értéke a json_decode -nak.
A kérdés, hogy miért?

A json_last_error() visszatérése 3, azaz Unexpected control character found
Próbáltam, hogy a $rawData -ra ráeresztek egy stripslashes -t, de nem segített.
Azt is próbáltam, hogy kiszedem a bom -ot így:
  1. $rawData = str_replace("\xEF\xBB\xBF",'',$rawData);  
Sajnos ez sem segített.


Van valakinek valami jó ötlete, hogy mi lehet a gond?
 
1

Egy " hátracsúszott, a https

bamegakapa · 2014. Feb. 25. (K), 10.18
Egy " hátracsúszott, a https előtt most nincsen.

Ha ez csak typo, akkor valami van ott, ami nem látszik :). Próbáld dumpolni úgy, előtte ráraksz egy htmlentitiest, hátha HTML-t is küldenek vele, bár nem tudom, miért tennék. Vizsgáld meg a string hosszát, és hasonlítsd össze a tényleges hosszal (amire számítasz).
2

typo és dump

DarkHcK · 2014. Feb. 25. (K), 10.31
Az csak typo. Azt javítom is itt a leírásban. A hosszába tényleg van valami, mert ha bemásolom notepad++ ba, akkor a length: 116, viszont a var_dump azt írja, hogy 124.
3

Megoldás

DarkHcK · 2014. Feb. 25. (K), 10.49
Valami miatt a válasz elé bekerült néhány láthatatlan karakter, amit a trim le tud szedni. A chrome postman írta ki egyedül, s így jöttünk rá.
4

JSON

janoszen · 2014. Feb. 25. (K), 18.12
Ha PHP-ban JSON-t dekodolsz, akkor _mindig_ erdemes hasznalni a json_last_error fuggvenyt hogy megtudd, volt-e hiba a dekodolasban.

A sajat hasznalatu frameworkomben ezt az osztalyt hasznalom JSON muveletekre:
  1. <?php  
  2.   
  3. namespace OGF\JSON;  
  4.   
  5. use OGF\Lang\Object;  
  6.   
  7. /** 
  8.  * Utility class to make handling JSON encoding and decoding easier. 
  9.  * 
  10.  * Encoding: 
  11.  *  
  12.  * \OGF\JSON\JSON::encode($yourdata); 
  13.  * 
  14.  * You may also pass a bitmask of encoding options: 
  15.  * 
  16.  * \OGF\JSON\JSON::encode( 
  17.  *     $yourdata, 
  18.  *     \OGF\JSON\JSON::ENCODE_HEX_QUOT 
  19.  * ); 
  20.  * 
  21.  * Decoding: 
  22.  * 
  23.  * \OGF\JSON\JSON::decode($jsonstring); 
  24.  * 
  25.  * If you want to cast large integers as strings instead of floats, and also pass a depth value: 
  26.  * 
  27.  * \OGF\JSON\JSON::decode( 
  28.  *     $jsonstring, 
  29.  *     128, 
  30.  *     \OGF\JSON\JSON::DECODE_BIGINT_AS_STRING 
  31.  * ); 
  32.  */  
  33. class JSON extends Object {  
  34.     /** 
  35.      * All " are converted to \u0022. 
  36.      */  
  37.     const ENCODE_HEX_QUOT          = \JSON_HEX_QUOT;  
  38.   
  39.     /** 
  40.      * All < and > are converted to \u003C and \u003E. 
  41.      */  
  42.     const ENCODE_HEX_TAG           = \JSON_HEX_TAG;  
  43.   
  44.     /** 
  45.      * All &s are converted to \u0026. 
  46.      */  
  47.     const ENCODE_HEX_AMP           = \JSON_HEX_AMP;  
  48.   
  49.     /** 
  50.      * All ' are converted to \u0027. 
  51.      */  
  52.     const ENCODE_HEX_APOS          = \JSON_HEX_APOS;  
  53.   
  54.     /** 
  55.      * Encodes numeric strings as numbers. 
  56.      */  
  57.     const ENCODE_NUMERIC_CHECK     = \JSON_NUMERIC_CHECK;  
  58.   
  59.     /** 
  60.      * Use whitespace in returned data to format it. 
  61.      */  
  62.     const ENCODE_PRETTY_PRINT      = \JSON_PRETTY_PRINT;  
  63.   
  64.     /** 
  65.      * Don't escape /. 
  66.      */  
  67.     const ENCODE_UNESCAPED_SLASHES = \JSON_UNESCAPED_SLASHES;  
  68.   
  69.     /** 
  70.      * Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient 
  71.      * of the output is expecting an object and the array is empty. 
  72.      */  
  73.     const ENCODE_FORCE_OBJECT      = \JSON_FORCE_OBJECT;  
  74.   
  75.     /** 
  76.      * Encode multibyte Unicode characters literally (default is to escape as \uXXXX). 
  77.      */  
  78.     const ENCODE_UNESCAPED_UNICODE = \JSON_UNESCAPED_UNICODE;  
  79.   
  80.     /** 
  81.      * Encodes large integers as their original string value. 
  82.      */  
  83.     const DECODE_BIGINT_AS_STRING  = \JSON_BIGINT_AS_STRING;  
  84.   
  85.     /** 
  86.      * Encodes a PHP variable as a JSON string. 
  87.      * 
  88.      * @see    self::ENCODE_* 
  89.      * 
  90.      * @param  mixed $data 
  91.      * @param  int   $options 
  92.      * 
  93.      * @return string 
  94.      * 
  95.      * @throws JSONSyntaxError 
  96.      * @throws JSONInvalidControlCharacter 
  97.      * @throws JSONInvalidUTF8Error 
  98.      * @throws JSONInvalidError 
  99.      * @throws JSONMaximumStackDepthError 
  100.      */  
  101.     public static function encode($data$options = 0) {  
  102.         $string = \json_encode($data$options);  
  103.         switch (\json_last_error()) {  
  104.             case JSON_ERROR_DEPTH:  
  105.                 throw new JSONMaximumStackDepthError($data);  
  106.                 break;  
  107.             case JSON_ERROR_STATE_MISMATCH:  
  108.                 throw new JSONInvalidError($data);  
  109.                 break;  
  110.             case JSON_ERROR_CTRL_CHAR:  
  111.                 throw new JSONInvalidControlCharacter($data);  
  112.                 break;  
  113.             case JSON_ERROR_SYNTAX:  
  114.                 throw new JSONSyntaxError($data);  
  115.                 break;  
  116.             case JSON_ERROR_UTF8:  
  117.                 throw new JSONInvalidUTF8Error($data);  
  118.                 break;  
  119.         }  
  120.   
  121.         return $string;  
  122.     }  
  123.   
  124.     /** 
  125.      * Decodes a string into a PHP array. 
  126.      * 
  127.      * @see    self::DECODE_* 
  128.      * 
  129.      * @param  string $string 
  130.      * @param  int    $depth 
  131.      * @param  int    $options 
  132.      * 
  133.      * @return string 
  134.      * 
  135.      * @throws JSONSyntaxError 
  136.      * @throws JSONInvalidControlCharacter 
  137.      * @throws JSONInvalidUTF8Error 
  138.      * @throws JSONInvalidError 
  139.      * @throws JSONMaximumStackDepthError 
  140.      */  
  141.     public static function decode($string$depth = 512, $options = 0) {  
  142.         $data = \json_decode($string, true, $depth$options);  
  143.         switch (\json_last_error()) {  
  144.             case JSON_ERROR_DEPTH:  
  145.                 throw new JSONMaximumStackDepthError($string);  
  146.                 break;  
  147.             case JSON_ERROR_STATE_MISMATCH:  
  148.                 throw new JSONInvalidError($string);  
  149.                 break;  
  150.             case JSON_ERROR_CTRL_CHAR:  
  151.                 throw new JSONInvalidControlCharacter($string);  
  152.                 break;  
  153.             case JSON_ERROR_SYNTAX:  
  154.                 throw new JSONSyntaxError($string);  
  155.                 break;  
  156.             case JSON_ERROR_UTF8:  
  157.                 throw new JSONInvalidUTF8Error($string);  
  158.                 break;  
  159.         }  
  160.   
  161.         return $data;  
  162.     }  
  163. }  
MIT licenc, yada yada, hasznald egeszseggel.