Problems with facebook API



我在使用Facebook应用程序时遇到问题,当我尝试发送消息以使用机器人接收时,出现此错误。如果您需要 php 脚本来更好地理解错误,请询问。

<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://4eecef78.ngrok.io/whatsapp/">here</a>.</p>
</body></html>

这是我的索引.php脚本

<?php
$access_token = "EAANXQoEiXdsBAAeeSGCv80gkk8tzo4hHYUMbfW3xCu8en9QjaLrDpT2Pj2ChO9668stns6xNao5NGyNKldw5v9yXUCnc1qvHSCuZCvhChoP59uiMZC9k27lMSRAD2ZB0qro5ABrptXHHZACHs3cDLlNggVBMLwrqWCgSjKz8lQZDZD";
$verify_token = "fb";
$hub_verify_token = null;
 //verifica del token con fb per avere accesso all'app
if(isset($_REQUEST['hub_challenge'])) {
    $challenge = $_REQUEST['hub_challenge'];
    $hub_verify_token = $_REQUEST['hub_verify_token'];
}

if ($hub_verify_token === $verify_token) {
    echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true); //converto json in arrivo in array php
$sender = $input['entry'][0]['messaging'][0]['sender']['id']; //prendo id sender
$message = $input['entry'][0]['messaging'][0]['message']['text']; //prendo testo

 $message_to_reply = 'Huh! what do you mean?';
 //messaggio di bot
//da qui in poi si può fare una funzione di send message 
//
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
//formatto la risposta per fb mettendo id di ricezione e messaggio di risposta
$response= [
    'recipient' => [ 'id' => $sender ],
    'message' => [ 'text' => $message_to_reply]
];
$ch = curl_init($url); //inizializzo curl per fare la post

$jsonDataEncoded = json_encode($response); //traduco la response in json
//indico al curl che sarà una post
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); //appendo la risposta
//setto content type e variabili di sicurezza che non avendo tunnel ssl vanno settate cosi 
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_TIMEOUT, 100);
//invio il curl finchè arriva in 'posta un messaggio'
if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
   //print var_dump($response);//stampo in console il testo di risposta da parte di fb per verifica
}

?>

问题解决了,我使用的是PHP dev服务器而不是Web服务器。巨大的错误,但最后我明白了!!

最新更新