Jump to content

Recommended Posts

Posted

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. 

Posted

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.

Posted
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.

Posted
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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...