I'm using the Rocketium API to auto generate videos.
To prepare the "scenes" used in the video, I built a JSON string from rows in a table of my database:
foreach ($products as $product) {
if ($product['image_one_url']) {
$product_image = $product['image_one_url'];
} else {
$product_image = 'no_image.png';
}
$string[] = [
"text" => $product['product_name'],
"image" => $product_image
];
}
$string = json_encode($string, JSON_UNESCAPED_SLASHES);
$string = addslashes($string);
Here is how $string
outputs:
[{\"text\":\"Definitions\",\"image\":\"vesa_definitions.jpg\"},{\"text\":\"Persona\",\"image\":\"vesa_persona.jpg\"},{\"text\":\"Universal Invitation\",\"image\":\"vesa_universal_invitation.jpg\"},{\"text\":\"Immortal\",\"image\":\"vesa_immortal.jpg\"},{\"text\":\"Birth\",\"image\":\"vesa_birth.jpg\"},{\"text\":\"Red Eye\",\"image\":\"vesa_red_eye.jpg\"},{\"text\":\"Lakshmi (Resurrection)\",\"image\":\"vesa_lakshmi.jpg\"},{\"text\":\"T(r)opical\",\"image\":\"vesa_tropical.jpg\"},{\"text\":\"Fork and Flip\",\"image\":\"vesa_fork_and_flip.jpg\"},{\"text\":\"Stereoscopic\",\"image\":\"vesa_stereoscopic.jpg\"},{\"text\":\"I AM SATOSHI NAKAMOTO\",\"image\":\"vesa_takeshi_nakamoto.jpg\"}]
Now I'm taking this string and trying to plug it in here with the interpolated variable:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"videoBackground\": \"background.jpg\", \"audio_mood\": \"inspirational\", \"logoImage\": \"logo.png\", \"title\": \"Products\", \"themeId\": \"5a15310cabc5e17e6bf29525\", \"scenes\": {$string}}");
This is not working for me for some reason, although when I compare my JSON string to a working example, it looks the same format:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"videoBackground\": \"background.jpg\", \"audio_mood\": \"inspirational\", \"logoImage\": \"logo.png\", \"title\": \"Products\", \"themeId\": \"5a15310cabc5e17e6bf29525\", \"scenes\": [{\"text\" : \"{Hello there\", \"image\" : \"https://rocketium.com/videos/1234567890/resized/abcdefgh.mp4\", \"fontSize\" : \"14px\"}, { \"text\" : \"Slide 2 goes here\", \"image\" : \"https://rocketium.com/videos/1234567890/resized/abcdefgh.mp4\" }, { \"text\" : \"Slide 3 here\", \"image\" : \"https://rocketium.com/videos/1234567890/resized/abcdefgh.mp4\" }, { \"text\" : \"Slide 4 here\", \"image\" : \"image_goes_here.jpg\" }]}");
I've added slashes and everything. Is this an issue with the interpolated variable or something else I'm missing?
Instead of trying to cram one string inside another, manually escaping quotes and hoping for the best, work with data structures and only convert to JSON when you're done.
Something like this:
foreach ($products as $product) {
if ($product['image_one_url']) {
$product_image = $product['image_one_url'];
} else {
$product_image = 'no_image.png';
}
$string[] = [
"text" => $product['product_name'],
"image" => $product_image
];
}
$template = json_decode("{\"videoBackground\": \"background.jpg\", \"audio_mood\": \"inspirational\", \"logoImage\": \"logo.png\", \"title\": \"Products\", \"themeId\": \"5a15310cabc5e17e6bf29525\"}");
$template['scenes'] = $string;
// Now you can encode the whole thing to JSON in one go
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($template));