<?php

// https://stackoverflow.com/questions/52943550/add-query-parameters-to-link-in-firebase-dynamic-link
// https://firebase.google.com/docs/dynamic-links/rest

function shorten_URL ($longUrl,$code) {
  $key = 'AIzaSyDWvLahdsUx-cxdbWdO_lK1mGlbjFmlTWQ';
  $url = 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=' . $key;
  $data = array(
     "longDynamicLink"=>"https://questcompendium.page.link?apn=com.questcompendium&ibi=com.questcompendium&link=".$longUrl.$code,
     // "dynamicLinkInfo" => array(
     // 	// "domainUriPrefix"=>"https://questcompendium.page.link",
     //  //   "dynamicLinkDomain" => $longUrl.$code,
     //  //   "link" => $longUrl.$code,
     // ),
     "suffix"=> array(
    	"option"=>"UNGUESSABLE"
     )
  );

  $headers = array('Content-Type: application/json');

  $ch = curl_init ();
  curl_setopt ( $ch, CURLOPT_URL, $url );
  curl_setopt ( $ch, CURLOPT_POST, true );
  curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
  curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

  $data = curl_exec ( $ch );
  curl_close ( $ch );

  $short_url = json_decode($data);
  if(isset($short_url->error)){
      return $short_url->error->message;
  } else {
      return $short_url->shortLink;
  }

}

// Call the function with the URL
// $randNo = 11;
// $shortLink = shorten_URL('http://newsnytro.com/ci/hotel-compendium/preprod/','?token='.$randNo);
// $shortLink = $shortLink.'?token='.$randNo;
$shortLink = 'https://questcompendium.page.link/sjs1frdQN3y88jaJA?token=11';

$shortLink = $shortLink;


?>
<div style="display: flex;">
	<div style="width: 50%;">
		<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=<?php echo $shortLink ?>&choe=UTF-8">
	</div>
	<div>
		<?php echo $shortLink; ?>
	</div>
</div>