注入带有插值变量的 JSON 字符串不起作用
Injecting a JSON string with interpolated variable not working
我正在使用 Rocketium API 自动生成视频。
为了准备视频中使用的 "scenes",我根据数据库 table 中的行构建了一个 JSON 字符串:
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);
下面是 $string
输出的方式:
[{\"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\"}]
现在我正在使用这个字符串并尝试使用插值变量将其插入此处:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"videoBackground\": \"background.jpg\", \"audio_mood\": \"inspirational\", \"logoImage\": \"logo.png\", \"title\": \"Products\", \"themeId\": \"5a15310cabc5e17e6bf29525\", \"scenes\": {$string}}");
出于某种原因,这对我不起作用,尽管当我将 JSON 字符串与工作示例进行比较时,它看起来是相同的格式:
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\" }]}");
我添加了斜杠和所有内容。这是插值变量的问题还是我遗漏的其他问题?
与其尝试将一个字符串塞进另一个字符串,手动转义引号并希望获得最佳效果,不如使用数据结构并在完成后才转换为 JSON。
像这样:
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));
@chris 是对的,我只是想告诉你为什么你的代码不起作用。因为 你实际上向 json 字符串注入了额外的反斜杠 ,而在工作示例中,没有真正的反斜杠(它们只是出现在代码中告诉 PHP下一个字符是双引号而不是右引号)
这个字符串 "\""
只包含一个双引号,而这个字符串 '\"'
包含一个反斜杠和一个双引号
工作示例中的场景属性实际上包含这个
$scenes = '[{"text":"Definitions","image":"vesa_definitions.jpg"}]';
但这就是你使用addslashes()
所做的
$scenes = '[{\"text\":\"Definitions\",\"image\":\"vesa_definitions.jpg\"}]';
我正在使用 Rocketium API 自动生成视频。
为了准备视频中使用的 "scenes",我根据数据库 table 中的行构建了一个 JSON 字符串:
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);
下面是 $string
输出的方式:
[{\"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\"}]
现在我正在使用这个字符串并尝试使用插值变量将其插入此处:
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"videoBackground\": \"background.jpg\", \"audio_mood\": \"inspirational\", \"logoImage\": \"logo.png\", \"title\": \"Products\", \"themeId\": \"5a15310cabc5e17e6bf29525\", \"scenes\": {$string}}");
出于某种原因,这对我不起作用,尽管当我将 JSON 字符串与工作示例进行比较时,它看起来是相同的格式:
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\" }]}");
我添加了斜杠和所有内容。这是插值变量的问题还是我遗漏的其他问题?
与其尝试将一个字符串塞进另一个字符串,手动转义引号并希望获得最佳效果,不如使用数据结构并在完成后才转换为 JSON。
像这样:
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));
@chris 是对的,我只是想告诉你为什么你的代码不起作用。因为 你实际上向 json 字符串注入了额外的反斜杠 ,而在工作示例中,没有真正的反斜杠(它们只是出现在代码中告诉 PHP下一个字符是双引号而不是右引号)
这个字符串 "\""
只包含一个双引号,而这个字符串 '\"'
包含一个反斜杠和一个双引号
工作示例中的场景属性实际上包含这个
$scenes = '[{"text":"Definitions","image":"vesa_definitions.jpg"}]';
但这就是你使用addslashes()
$scenes = '[{\"text\":\"Definitions\",\"image\":\"vesa_definitions.jpg\"}]';