0

What am I doing ?

I have a dynamically created html division containing the score of a game played by user which user can share on social media.

For sharing I have meta tag e.g. facebook

<meta property="og:image" content= "<?php echo $img_url; ?>"/>

Where $img_url is the link of the screenshot of dynamically created html division.

test.php -- where my above mentioned meta tag is placed and I want to take screenshot of html div if $img_url is not set.

 <?php
  session_start();
  
  $img_url = $_POST['img_url'];
  
  if(!isset($url)) {

   /* <script>
    function capture() {    

     html2canvas will give var img.
     Which will be POSTED to testsave.php to save it in the server.
     
     }
   </script>  */  
 }
?>
<html>

<!--dynamically created html division -->

</html>

testsave.php -- which will take the posted value from test.php and save the screenshot image to server and will send back the $img_url to test.php

<?php
 session_start();
 
/*Get the base-64 string from data

Decode the string
 
Save the image

function predefined for random string*/

$img_url = $random_str.".png"; 

file_put_contents($img_url, $unencodedData);

$_SESSION['img_url'] = $img_url ;

?>

What my problem is ?

When facebook scrapes a paricular page , it doesn't take session values or any values from other page. So I can't send img_val from testsave.php to another page because scraper won't read POSTED values.

So, what am I doing is when scraper will scrape test.php then if $img_val is not set I want my code to take the screenshot and post js variable var img to testsave.php to save the image, which will then post back the value of $img_val to test.php.

I know there may be many mistakes in above code like I can't put js inside php. But I tried it many way but couldn't figure it out.

Can you please suggest what can I do to make the code work or can you suggest any other way to do it ?

nm__
  • 11
  • 2

3 Answers3

1

You have to echo out HTML from PHP

<?php
  session_start();

  $img_url = $_POST['img_url'];

  if(!isset($url)) {

   echo "<script>
    function capture() {    

     //your js code here

     }
   </script>";  
 }
?>
<html>

<!--dynamically created html division -->

</html>
Francis Sunday
  • 77
  • 1
  • 11
1

One of the solutions is to use here-document to assign the whole HTML page to PHP variable and later echo/print it where it matches your needs, and you can include the Javascript codes there as well

Example "this example doesn't include the image thing you mentioned but you can use any Javascript codes"

<?php

$variable=<<<_END
<html>
<p>Some HTML line...</p> 
<script>
//you can write the Javascript codes you need here
 console.log('Find this sentence at console !')
</script>
</html>
_END;

echo $variable;

?>

  • This won’t help one bit in regard to what needs to be achieved here. The Facebook scraper will still not execute any of that JavaScript. – 04FS Feb 18 '19 at 14:39
  • Your support give me guideline to what I am trying to develop in my script. Thanks – Chibueze Agwu Feb 03 '21 at 10:18
1

In order to deliver it in browser, we have to construct JS code as a string inside the PHP code

eg. Test.phtml - where i am calling a js function under some condition.

<body>
 <?php if($this->emptyData){ ?>
    <div id="someTestDialog">
        <!-- call js function below -->
        <?php
            echo '<script type="text/javascript">testDialog();</script>';
        ?>
    </div>
<?php } ?>

</body>
<script>
function testDialog() {
    return $("<div class='dialog' title='Alert'><p>Hello World</p></div>")
        .dialog({
            resizable: false,
            height:90,
            width:90,
            modal: true,
            dialogClass: 'no-close success-dialog',
            buttons: {
                "OK": function() {
                $( this ).dialog( "close" );
            }
         }
    });}
$(document).ready(function() {
    var text = "some code here" });    </script>
Ashwathama
  • 86
  • 6