sobrenome Posted December 12, 2020 Posted December 12, 2020 This is my code on a Page template: {{$videos = $record->customFieldDisplayByKey('youtube_json');}} {{$videos = json_decode($videos, true);}} {{print_r($videos);}} I have tested the content of the field and it is a valid json format. But when I decode it to use it as a php array, it is empty.
sobrenome Posted December 13, 2020 Author Posted December 13, 2020 I have checked json_last_error() and number 4 pop out: syntax error. So I have adopted a different approach to cache the array API data: serialize() and unserialize(). But I had also the same syntax issues, that were solved by base64_encode() and base64_decode(). To save in the database: {{$videos_serialized = base64_encode(serialize($videos));}} To use it again: {{$videos = unserialize(base64_decode($videos));}} Hope this helps someone using json APIs and caching the results. IPCommerceFan 1
Stuart Silvester Posted December 13, 2020 Posted December 13, 2020 5 hours ago, sobrenome said: I have checked json_last_error() and number 4 pop out: syntax error. So I have adopted a different approach to cache the array API data: serialize() and unserialize(). But I had also the same syntax issues, that were solved by base64_encode() and base64_decode(). To save in the database: {{$videos_serialized = base64_encode(serialize($videos));}} To use it again: {{$videos = unserialize(base64_decode($videos));}} Hope this helps someone using json APIs and caching the results. You should avoid using serialize, it has inherent security issues especially when using it with user generated content.. See warning - https://www.php.net/manual/en/function.unserialize.php I don't know how you're generating the 'youtube_json' value, but it would be better/safer to look into why the syntax is invalid and address that instead. sobrenome 1
sobrenome Posted December 14, 2020 Author Posted December 14, 2020 13 hours ago, Stuart Silvester said: don't know how you're generating the 'youtube_json' It is the json api response from YouTube for videos from channels. It is not a user generated content, it is content delivered by YouTube api.
Recommended Posts