手動ループを作成して、開始しているプロジェクトで別のAPIで使用されているjsonを作成しました。下記をご覧ください。JSONを手動で作成するPHPループ
問題は、APIが私のjson出力を認識していないことです。私のループの結果をチェックし、それは正常に見えます。
私の結果(エコー)を直接コピー&ペーストすれば正常に動作しますが、私のループでは動作しません。誰もが何かイデアを持っていますか?
foreach ($array['hits'] as $key => $value) {
$message = $message.'{
"title":"'.$value['Title'].'",
"image_url":"'.$value['image'].'",
"subtitle":"'.substr($value['Detail'],0,120).'",
"buttons":[
{
"type":"web_url",
"url":"'.SITE_ROOT_URL.$value['URL'].'?utm_source=chatbot",
"title":"Leia mais"
}
]
},';
}
$message = '{"messages": [
{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":['.rtrim($message,",").']
}
}
}
]
}';
echo $message;
でvar_exportの出力($配列[ 'ヒット'])のようになります。
array (0 => array ('ID' => '69', 'Title' => 'This is an example', 'URL' => 'example/1', 'Detail' => 'Some description here...', 'image' => 'image1.png', 'objectID' => '75877631')), 1 => array ....
。 –
なぜ 'json_encode()'を使いたくないのですか? – Barmar
@JayBlanchard私はJSONを手作業で作成すると言っているほど遠くには行かないでしょう - それを呼び出すいくつかの本物のケースがありますが、確かにそうではありません。 'json_encode'は[特定の状況で誤った種類の配列を出力する]ことができます(https://eval.in/727141)。 –