2017-05-26 11 views
-1

Facebookのアプリに問題があります。ボットで受信するメッセージを送信しようとすると、このエラーが発生します。 エラーをよりよく理解するためにPHPスクリプトが必要な場合は、尋ねてください。フェイスブックAPIの問題

<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 
$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 
} 



?> 
+0

私たちは助けることができません –

+0

は、PHPスクリプトのコードを追加 – cancer

答えて

0

問題の私のindex.phpスクリプトですが、私はPHP devのサーバーの代わりに、Webサーバを使用していました。巨大な間違いですが、最終的に私はそれを得ました!