我如何使用PHP的JavaScript代码?

问题描述 投票:0回答:2

我在做什么 ?

我有一个dynamically created html division,其中包含用户可以在社交媒体上分享的游戏分数。

为了共享,我有元标记,例如Facebook的

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

其中$img_urldynamically created html division截图的链接。

test.php - 我上面提到的元标记放在哪里,如果没有设置html div,我想截取$img_url的截图。

 <?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 - 将从test.php获取已发布的值并将屏幕截图保存到服务器并将$img_url发送回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 ;

?>

我的问题是什么?

当facebook刮擦一个特定的页面时,它不需要session values或其他页面的任何值。所以我不能将testsave.php中的img_val发送到另一个页面,因为scraper不会读取POSTED值。

那么,我在做什么是当刮刀刮掉test.php然后如果没有设置$img_val我希望我的代码截取屏幕并将js变量var img发布到testsave.php以保存图像,然后将$img_val的值发回test.php

我知道上面的代码可能有很多错误,比如我不能把js放在php里面。但我尝试了很多方法,但无法弄明白。

您能否建议我可以做些什么来使代码工作,或者您可以建议其他任何方式来做到这一点?

javascript php html2canvas
2个回答
1
投票

你必须从PHP回显HTML

<?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>

0
投票

其中一个解决方案是使用here-document将整个HTML页面分配给PHP变量,然后在符合您需求的地方回显/打印它,您也可以在那里包含Javascript代码

示例“此示例不包含您提到的图像,但您可以使用任何Javascript代码”

<?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;

?>

© www.soinside.com 2019 - 2024. All rights reserved.