{"id":3562,"date":"2022-06-06T08:07:10","date_gmt":"2022-06-06T08:07:10","guid":{"rendered":"https:\/\/blog.ultramsg.com\/ricevere-messaggi-whatsapp-usando-php-e-webhook\/"},"modified":"2022-06-06T08:07:11","modified_gmt":"2022-06-06T08:07:11","slug":"ricevere-messaggi-whatsapp-usando-php-e-webhook","status":"publish","type":"post","link":"https:\/\/blog.ultramsg.com\/it\/ricevere-messaggi-whatsapp-usando-php-e-webhook\/","title":{"rendered":"Ricevi messaggi WhatsApp utilizzando PHP e webhook"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"introduction\">introduzione<\/h2>\n\n<p>In questo tutorial creeremo una pagina per gestire i messaggi WhatsApp in arrivo utilizzando PHP.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"dependencies\">Dipendenze<\/h2>\n\n<p>per scopi di sviluppo locale \u00e8 necessario un servizio di tunneling. Questo esempio usa <a href=\"https:\/\/ngrok.com\/\" rel=\"nofollow noopener\" target=\"_blank\">ngrok<\/a> , puoi scaricare ngrok da <a href=\"https:\/\/ngrok.com\/download\" data-type=\"URL\" data-id=\"https:\/\/ngrok.com\/download\" rel=\"nofollow noopener\" target=\"_blank\">qui<\/a> .<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"example\">Esempio<\/h2>\n\n<p>Questo \u00e8 un esempio molto semplice, con il server che registra il corpo della richiesta nel file log.txt.<\/p>\n\n<p>Il corpo contiene le informazioni complete sul webhook inviate dall&#8217;istanza WhatsApp dell&#8217;API Ultramsg.<\/p>\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">$data = file_get_contents(\"php:\/\/input\");\n$event = json_decode($data, true);\nif(isset($event)){\n\t\/\/Here, you now have event and can process them how you like e.g Add to the database or generate a response\n\t$file = 'log.txt';  \n\t$data =json_encode($event).\"\\n\";  \n\tfile_put_contents($file, $data, FILE_APPEND | LOCK_EX);\n<\/code>}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"on-server-usage\">Sull&#8217;utilizzo del server<\/h2>\n\n<p>Salva il file di esempio sopra come test.php e caricalo sul tuo server l&#8217;URL di Webhook sar\u00e0:<\/p>\n\n<pre class=\"wp-block-code\"><code><a href=\"http:\/\/your-server\/webhook.php\"><code data-enlighter-language=\"powershell\" class=\"EnlighterJSRAW\">http:\/\/your-server\/test.php<\/code><\/a><\/code><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"on-local-usage-with-ngrok\">Su utilizzo locale con ngrok<\/h2>\n\n<p>Salva il file di esempio sopra come test.php nel tuo localhost e l&#8217;URL sar\u00e0:<\/p>\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"powershell\" class=\"EnlighterJSRAW\">http:\/\/localhost\/test.php<\/code><\/code><\/pre>\n\n<p>Inizia ngrok:<\/p>\n\n<pre class=\"wp-block-code\"><code><code data-enlighter-language=\"powershell\" class=\"EnlighterJSRAW\">ngrok http 80<\/code><\/code><\/pre>\n\n<p>Dopo questo, dovresti vedere a<br\/>*.ngrok.io URL.<br\/>per esempio :<br\/>https:\/\/7647-115-83-121-164.ngrok.io<br\/>sostituire localhost con<br\/>URL di ngrok.io<br\/>l&#8217;URL sar\u00e0:<br\/>https:\/\/7647-115-83-121-164.ngrok.io\/webhook.php<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"what-s-next\">Qual \u00e8 il prossimo?<\/h2>\n\n<p>incolla il tuo URL nelle impostazioni dell&#8217;istanza.<br\/>Dovresti essere in grado di ricevere webhook ora e puoi vederli nel file log.txt.<\/p>\n\n<h2 class=\"wp-block-heading\" id=\"example-json-response\">ESEMPIO DI RISPOSTA JSON <\/h2>\n\n<pre class=\"wp-block-code\"><code>{\n  \"event_type\": \"message_received\",\n  \"instanceId\": \"90\",\n  \"data\": {\n    \"id\": \"false_10172127174@c.us_7ECAED9EB68D3474BE591443134C2E3F\",\n    \"from\": \"10172127174@c.us\",\n    \"to\": \"10172127175@c.us\",\n    \"ack\": \"pending\",\n    \"type\": \"chat\",\n    \"body\": \"I can't send a message using php code\\nCan you help me\",\n    \"fromMe\": false,\n    \"isForwarded\": false,\n    \"time\": 1643311467\n  }\n}<\/code><\/pre>\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Receive WhatsApp messages using PHP | WhatsApp api Webhook\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/TfQOclLMIng?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n<h2 class=\"wp-block-heading\" id=\"useful-links\">link utili<\/h2>\n\n<ul class=\"wp-block-list\"><li>Documentazione <span style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, \" segoe=\"\" ui=\"\" helvetica=\"\" arial=\"\" sans-serif=\"\" color=\"\" emoji=\"\" symbol=\"\">completa<\/span> <a style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, \" segoe=\"\" ui=\"\" helvetica=\"\" arial=\"\" sans-serif=\"\" color=\"\" emoji=\"\" symbol=\"\" href=\"https:\/\/docs.ultramsg.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">dell&#8217;API di Whatsapp<\/a> <span style=\"font-family: -apple-system, system-ui, BlinkMacSystemFont, \" segoe=\"\" ui=\"\" helvetica=\"\" arial=\"\" sans-serif=\"\" color=\"\" emoji=\"\" symbol=\"\">.<\/span><\/li><li>invia messaggi <a href=\"https:\/\/blog.ultramsg.com\/send-whatsapp-message-by-whatsapp-api-using-php\/\">API WhatsApp<\/a> utilizzando PHP.<\/li><\/ul>\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>introduzione In questo tutorial creeremo una pagina per gestire i messaggi WhatsApp in arrivo utilizzando &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Ricevi messaggi WhatsApp utilizzando PHP e webhook\" class=\"read-more button\" href=\"https:\/\/blog.ultramsg.com\/it\/ricevere-messaggi-whatsapp-usando-php-e-webhook\/#more-3562\" aria-label=\"Per saperne di pi\u00f9 su Ricevi messaggi WhatsApp utilizzando PHP e webhook\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":3420,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[279,277,278],"class_list":["post-3562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-api-di-whatsapp","tag-php-it","tag-tutorial-it","tag-webhook-it","infinite-scroll-item","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-25","no-featured-image-padding"],"_links":{"self":[{"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/posts\/3562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/comments?post=3562"}],"version-history":[{"count":1,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/posts\/3562\/revisions"}],"predecessor-version":[{"id":3563,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/posts\/3562\/revisions\/3563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/media\/3420"}],"wp:attachment":[{"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/media?parent=3562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/categories?post=3562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ultramsg.com\/it\/wp-json\/wp\/v2\/tags?post=3562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}