J'utilise chatbot messager avec script ci-dessous de Mr Nadeem Manzoor's tutorialobjet JSON de chatbot messager en PHP
Quand je reçois objet JSON en ligne 15: $input = json_decode(file_get_contents('php://input'), true);
Je veux sauver objet entier comme un texte dans le fichier txt . Comment est-ce possible? J'apprécierai toute aide. Il y a ensemble du script:
<?php
/* validate verify token needed for setting up web hook */
if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'codeandpepper') {
echo $_GET['hub_challenge'];
return;
} else {
echo 'Invalid Verify Token';
return;
}
}
/* receive and send messages */
$input = json_decode(file_get_contents('php://input'), true);
if (isset($input['entry'][0]['messaging'][0]['sender']['id'])) {
$sender = $input['entry'][0]['messaging'][0]['sender']['id']; //sender facebook id
$message = $input['entry'][0]['messaging'][0]['message']['text']; //text that user sent
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=EAAQyP47lxSQBAPQSXyHhQ7y0d0MTQgTn4xaFEDEu5yrc4qo4t0FWbHZAfT7TFQq3HYZCUGVcTPZAaN2ZBNrUShl4Hr5qQd8cqBu0mfB0Al7NZAWjIjQC9UqXQLU5E9IsZBaQDIXglMcEBqNUyiidGJWRPPfvCmtg4vy2UgUhU8SQZDZD';
/*initialize curl*/
$ch = curl_init($url);
/*prepare response*/
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"You said, ' . $message . '"
}
}';
/* curl setting to send a json post data */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if (!empty($message)) {
$result = curl_exec($ch); // user will get the message
}
}
?>
Clarifiez s'il vous plaît: Vous voulez enregistrer la chaîne JSON dans un fichier, ou le tableau résultant de 'json_decode()'? Les deux sont assez simples – RiggsFolly
Je veux chaîne JSON, pas de tableau fait à partir de lui, mais si c'est simple, je voudrais voir les deux exemples :) –