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 ?

6
  • Have you tried to use ajax? Commented Oct 18, 2016 at 7:17
  • 2
    PHP is server side, you need to echo your script to HTML. Commented Oct 18, 2016 at 7:17
  • @Icewine If you are asking to use ajax to post the img val within the same same page. I did it but since ajax sends back the value to same page a bit later hence scraper is not able to scrape that value. Commented Oct 18, 2016 at 7:22
  • @JuanjoSalvador I didn't get what u said. can u please give a short example ? Commented Oct 18, 2016 at 7:24
  • 4
    Possible duplicate of how to write javascript code inside php Commented Oct 18, 2016 at 7:59

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

Comments

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;

?>

2 Comments

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.
Your support give me guideline to what I am trying to develop in my script. Thanks
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.