【问题标题】:How can I use javascript code with php?如何在 php 中使用 javascript 代码?
【发布时间】:2017-02-27 08:22:16
【问题描述】:

我在做什么?

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

为了分享,我有元标记,例如脸书

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

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

test.php -- 上面提到的元标记放置的位置,如果未设置$img_url,我想截取html div 的屏幕截图。

 <?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 到另一个页面,因为刮板不会读取 POSTED 值。

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

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

您能否建议我可以做些什么来使代码正常工作,或者您可以建议任何其他方法吗?

【问题讨论】:

  • 你试过用ajax吗?
  • PHP是服务器端,你需要echo你的脚本到HTML。
  • @Icewine 如果您要求使用 ajax 在同一页面中发布 img val。我做到了,但由于 ajax 稍后将值发送回同一页面,因此刮板无法刮取该值。
  • @JuanjoSalvador 我没听懂你说的。你能举一个简短的例子吗?

标签: javascript php html2canvas


【解决方案1】:

为了在浏览器中传递它,我们必须在 PHP 代码中将 JS 代码构造为字符串

例如。 Test.phtml - 我在某些条件下调用 js 函数。

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

【讨论】:

    【解决方案2】:

    其中一个解决方案是使用 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;
    
    ?>
    
    

    【讨论】:

    • 这对于这里需要实现的目标一点帮助也没有。 Facebook 抓取工具仍然不会执行任何 JavaScript。
    • 您的支持为我在脚本中尝试开发的内容提供了指导。谢谢
    【解决方案3】:

    你必须从 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>
    

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 2011-03-22
      • 2012-05-22
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多