<?php
ini_set("log_errors", 1);
ini_set("error_log", "error.log");
// API key that comes from bank in each psh message to verify sender d7846c24-73d6-452e-ad05-804a4ef3a501;
require_once './vendor/autoload.php';

use FacebookAds\Object\Lead;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;

// testing data
// inMobile test groupid 4b0f27ef-4f4f-4cd8-9580-cb8b178d6e0f

date_default_timezone_set( 'UTC' );
$dataToSave = '';
$date = new DateTime();

//  instantiate a new Facebook\Facebook service
$fb = new Facebook\Facebook([
  'app_id' => '807755569859311',
  'app_secret' => 'a833cc7ab46ea9b5b505bb51aa3268eb',
  'default_graph_version' => 'v10.0',
  'default_access_token' => 'EAALepjZCOiu8BAAXhKZCElK2vlUDZAw69VLAXTQIdKqyAkgda5Qwq3m87Mb4vjppvtklSh12YRQEDmGtI5D7kZAJWR6F3l5oEJZCZCsipZBUwxdEQ5lpCEEz8mDOKi1e7OAcAk9VOxGa0jYXQln0m1LOUoZA6MXozbdix4woAEfPLttxVJ5C6ALz', // optional, otherwise it can be used as second parameter in queries $fb->get('/me', '{access-token}');
  //'default_access_token' =>   'EAALepjZCOiu8BAAXhKZCElK2vlUDZAw69VLAXTQIdKqyAkgda5Qwq3m87Mb4vjppvtklSh12YRQEDmGtI5D7kZAJWR6F3l5oEJZCZCsipZBUwxdEQ5lpCEEz8mDOKi1e7OAcAk9VOxGa0jYXQln0m1LOUoZA6MXozbdix4woAEfPLttxVJ5C6ALz', // optional, otherwise it can be used as second parameter in queries $fb->get('/me', '{access-token}');
]);

// EXAMPLE OF USE
// Send a GET request
//$response = $fb->get('/me');

// Send a POST request
//$response = $fb->post('/me/feed', ['message' => 'Foo message']);

// Send a DELETE request
//$response = $fb->delete('/{node-id}');

// Full configuration options list:
/*$fb = new Facebook\Facebook([
  'app_id' => '{app-id}', //The ID of your Facebook app (required).
  'app_secret' => '{app-secret}', // The secret of your Facebook app (required).
  'default_access_token' => '{access-token}',
  'enable_beta_mode' => true,
  'default_graph_version' => 'v2.3',
  'http_client_handler' => 'guzzle',
  'persistent_data_handler' => 'memory', // Allows you to overwrite the default persistent data store.
  'url_detection_handler' => new MyUrlDetectionHandler(), // Allows you to overwrite the default URL detection logic.
  'pseudo_random_string_generator' => new MyPseudoRandomStringGenerator(),
]);*/
// END OF EXAMPLE OF USE


// check for the first verification request from facebook
if ($_SERVER['REQUEST_METHOD'] == 'GET') {

    foreach ($_REQUEST as $name => $value) {
        $dataToSave .= $name . ': ' . $value . "\n";
    }

    $hub_mode = $_REQUEST['hub_mode'];
    $hub_challenge = $_REQUEST['hub_challenge'];
    $hub_verify_token = $_REQUEST['hub_verify_token'];

    // endpoint can receive subscribe verify request
    if ($hub_verify_token === 'a1b2c3d4e5=11') {
        echo $hub_challenge;
    } else {
        // or it can receive working body for the handling
        $input = json_decode(file_get_contents('php://input'), true);
    }

}

//
try {
  // Get the \Facebook\GraphNodes\GraphUser object for the current user.
  // If you provided a 'default_access_token', the '{access-token}' is optional.
  $response = $fb->get('/me');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$me = $response->getGraphUser();
echo 'All the data returned from the Facebook server: ' . $me;
echo 'Logged in as ' . $me->getName();


/* make LEADGEN the API call */
echo '<br>Now starting lead retreival';
$access_token = 'EAALepjZCOiu8BAAIAB6B3a78sKnvjaJ620jTDzMDGECeCLSxfN5bBIupuAHJpMj33NIaJ6z48GtDGQZCOuycGqDALwhPdMuQ2MCmy76NRyDoOEPkfWap4jIwgvSJsYdsUYyBw3sGDZAJ3c2a2n6GxM6rXgwaDAeQSrXEgbYBxHZCk1iDe7g1dgfr6CLQTK68Rta1tlqwowZDZD';
$app_secret = 'a833cc7ab46ea9b5b505bb51aa3268eb';
$app_id = '807755569859311';
$lead_id = '467203884382404';

$retrieve_lead_url = "https://graph.facebook.com/v10.0/210986540779392/leads?access_token=EAALepjZCOiu8BAAXhKZCElK2vlUDZAw69VLAXTQIdKqyAkgda5Qwq3m87Mb4vjppvtklSh12YRQEDmGtI5D7kZAJWR6F3l5oEJZCZCsipZBUwxdEQ5lpCEEz8mDOKi1e7OAcAk9VOxGa0jYXQln0m1LOUoZA6MXozbdix4woAEfPLttxVJ5C6ALz&fields=created_time,id,ad_id,form_id,field_data";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $retrieve_lead_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$lead_response = curl_exec($ch);
curl_close ($ch);


//$graphNode = $lead_response->getGraphNode();

echo "<br>Now ending lead retreival for the $retrieve_lead_url with response:" .  json_encode($lead_response);
/* EO make LEADGEN the API call */

?>