Twitter event triggers without actual event

时间:2015-07-31 19:57:12

标签: javascript php twitter web

I am facing a tricky problem with my code and hope to get some help on this. Below is a snippet of my code:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.myurl.com/" data-text="My text" data-via="Via">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

<script>
    twttr.events.bind( 
          'tweet', 
              function (ev) {

        <?php
        $file = 'file.txt';
        $id = trim($_GET["id"]);
        $data = array("ID:$id", "CURRENT POSITION:$position", 'Twitter');
        file_put_contents($file, implode(" ",$data).PHP_EOL,FILE_APPEND);
        ?>
                            }
                    );
</script>

I can't understand why my function (ev) triggers every time the page loads. It should be only triggered once a tweet has been posted. Anyone knows where the problem lies?

1 个答案:

答案 0 :(得分:1)

您的功能不会触发,您的 PHP 会触发。在浏览器看到之前,PHP在服务器上进行评估。事实上,浏览器不知道如何处理PHP。这里发生的是你的用户加载页面,它由PHP处理,执行PHP代码,并且由于没有输出任何内容,因此该函数为空。浏览器看到的代码是:

<script>
twttr.events.bind( 
      'tweet', 
          function (ev) {

                        }
                );
</script>

如果您在浏览器中检查源代码,您应该看到该函数为空。