我在做什么 ?
我有一个dynamically created html division
,其中包含用户可以在社交媒体上分享的游戏分数。
为了共享,我有元标记,例如Facebook的
<meta property="og:image" content= "<?php echo $img_url; ?>"/>
其中$img_url
是dynamically 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
里面。但我尝试了很多方法,但无法弄明白。
您能否建议我可以做些什么来使代码工作,或者您可以建议其他任何方式来做到这一点?
你必须从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>
其中一个解决方案是使用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;
?>