Thanks!
This is an excerpt of the callback page that only works with php 5.6.
When I enable php 5.4, I get the error "Facebook SDK returned an error: No URL set!". I commented the line header("location: https://www.messenger.com/t/..."); for debugging reasons.
Code:
<?php
$fb = new Facebook\Facebook([
'app_id' => '...',
'app_secret' => '...',
'default_graph_version' => 'v2.6',
]);
//header("location: https://www.messenger.com/t/...");
}
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
Then, the callback page points to "https://www.messenger.com/t/..." messenger page, whose behavior is specified in the file webhook.php (below you have an excerpt)
Code:
<?php
ini_set("allow_url_fopen", 1);
require ("config.php");
session_start();
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === '...') {
echo $challenge;
}
else{
$json2 = fixJson (file_get_contents('php://input'));
$input = json_decode($json2, true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$postback = $input['entry'][0]['messaging'][0]['postback']['payload'];
$pagid = '...';
if ($sender != $pagid && ($postback=="Start" || $message == "test") ){
$ch = curl_init();
$url = 'https://graph.facebook.com/v2.6/'.$sender.'?fields=first_name,last_name&access_token='.ACCESS_TOKEN;
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$resp = curl_exec( $ch );
$info = curl_getinfo($ch);
writeLog("curl ".$resp. " sender ".$sender. " url ".$url." info: ".$info['http_code']);
writeLog("curl error ".curl_error($ch));
curl_close($ch);
}
}
function writeLog ($text) {
// function that writes the log file
}
function fixJson ($json) {
return (preg_replace ('/:\s?(\d{14,})/', ': "${1}"', $json));
}
?>
The curl call only works if I enable php 5.4. With php 5.6 curl_error() returns "Received HTTP code 403 from proxy after CONNECT".
Maybe I only need to change some configurations in php 5.6. One day it worked and the following day no more (with no changes in the code).
Thank you for your support