通过 php 脚本在外部文件中执行 Javascript

发布于 2024-12-08 15:30:42 字数 2182 浏览 0 评论 0原文

我一直在尝试创建一个 php 文件,它基本上是一个移动短代码消息。由于页面上的任何输出都会自动显示在移动短信中,因此我无法在页面上使用任何 html。但在页面上输出文本之前,我在执行一些谷歌分析 JavaScript 代码时遇到问题。我创建了一个外部文件并在那里编写了 javascript,并尝试通过curl 执行该文件,但curl 不执行javascript。所以我的主文件 SMSapi.php 的代码是这样的:

<?php
    if(isset($_GET['mobile'])){
        $number = $_GET['mobile'];
    }

    if(isset($_GET['text'])){
        $data = $_GET['text'];
    }

    $brand = "mybrand";
    $event = "MyEvent";
    $deal = "MyDeal";

    $url = "http://myurl.com/sms.php";

    $post_string = "brand={$brand}&event={$event}&deal={$deal}";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $success = curl_exec($ch);

    curl_close($ch);

    echo "Your discount code is XYZ123";
?>

sms.php 代码如下:

<?php 

if(isset($_POST) && !empty($_POST['event']) && !empty($_POST['brand']) && !empty($_POST['deal'])):
    $event = urldecode($_POST['event']);
    $brand = urldecode($_POST['brand']);
    $deal = urldecode($_POST['deal']);
?>
<html>
    <head>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-MyNum']);
        _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    _gaq.push(['_trackEvent', '<?php echo $event; ?>', '<?php echo $brand; ?>', '<?php echo $deal; ?>']);
</script>
</head>
</html>
<?php endif ?>

主 SMS api 文件向 sms.php 文件发出一个curl 请求,当文件执行时,html 和 javascript以文本形式返回,而不会发生任何执行。因此 JavaScript 会出现在短信中。

有没有办法通过 php 实现外部 url 和其中的所有 javascript?

I have been trying to create a php file which basically is a mobile shortcode message. Since any output on the page automatically gets shown in the mobile SMS, I cannot use any html on the page. But I am having a problem in executing some google analytics javascript code before the text is outputted on the page. I create an external file and wrote the javascript there and tried to execute the file via curl, but curl does not execute the javascript. So my code for the main file SMSapi.php is something like this:

<?php
    if(isset($_GET['mobile'])){
        $number = $_GET['mobile'];
    }

    if(isset($_GET['text'])){
        $data = $_GET['text'];
    }

    $brand = "mybrand";
    $event = "MyEvent";
    $deal = "MyDeal";

    $url = "http://myurl.com/sms.php";

    $post_string = "brand={$brand}&event={$event}&deal={$deal}";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $success = curl_exec($ch);

    curl_close($ch);

    echo "Your discount code is XYZ123";
?>

The sms.php code is as follows:

<?php 

if(isset($_POST) && !empty($_POST['event']) && !empty($_POST['brand']) && !empty($_POST['deal'])):
    $event = urldecode($_POST['event']);
    $brand = urldecode($_POST['brand']);
    $deal = urldecode($_POST['deal']);
?>
<html>
    <head>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-MyNum']);
        _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    _gaq.push(['_trackEvent', '<?php echo $event; ?>', '<?php echo $brand; ?>', '<?php echo $deal; ?>']);
</script>
</head>
</html>
<?php endif ?>

the main SMS api file makes a curl request to the sms.php file and while the file gets executed, the html and javascript gets returned back as text without any execution happening there. And hence the javascript shows up in the SMS.

Is there a way to implement a external url and all the javascripts in it there and there via php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疧_╮線 2024-12-15 15:30:42

我假设这是一个在 SMS 提供商的调用上运行的脚本,并且您需要在 Google Analytics 中记录此事件。

首先,我本以为通过登录数据库而不是分析来更容易地实现这一点。由于 javascript 在服务器端运行,因此除了事件之外,您不会获得任何额外信息。

如果您确实需要运行 Google 代码,那么您需要尝试搜索“服务器端 javascript google Analytics”。该解决方案将高度依赖于您正在运行的服务器平台以及已安装/可以安装的内容。

一个可能适合您的 PHP 的有趣链接是:

http://code.google.com/p/ serversidegoogleanalytics/

但我没有使用过它,所以不知道它的效果如何。

I am presuming this is a script that runs on a call from an SMS provider, and you are tying to log this event in Google Analytics.

Firstly, I would have thought this would more easily be accomplished by logging to a database, rather than to Analytics. As the javascript is running server side, you won't get any extra information other that the event.

If you really do need to run the Google code then you need to try searching on "server side javascript google analytics". The solution is going to be highly dependant on what server platform you are running and what is/can be installed.

One interesting link though which may work for your PHP is:

http://code.google.com/p/serversidegoogleanalytics/

but I haven't used it so don't know how well this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文