2011-10-05 73 views
0

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

<?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文件,使卷曲请求短信.php文件,当文件被执行时,html和javascript会以文本形式返回,而不会在那里发生任何执行。因此,JavaScript显示在SMS中。

有没有一种方法来实现一个外部URL和所有的JavaScript在那里和那里通过PHP?

+0

你知道[ob_start()](http://php.net/manual/ru/function.ob-start.php)和其他输出缓冲功能吗?他们可能会帮助你。 – J0HN

+0

输出缓冲区可以隐藏html,这是有用的,但也没有实现html和javascript这是问题,因为我需要的javascript在谷歌分析中输入条目 – Shiva

回答

0

我假设这是从短信提供商的呼叫运行的脚本,而你绑登录谷歌分析该事件。首先,我认为通过登录到数据库而不是登录到Analytics可以更轻松地完成此操作。由于JavaScript运行在服务器端,因此您不会获得其他任何其他该事件的额外信息。

如果你确实需要运行谷歌代码,那么你需要尝试搜索“服务器端JavaScript谷歌分析”。该解决方案将高度依赖于您正在运行的服务器平台以及可以安装哪些服务器平台。

一个有趣的链接,虽然这可能是你的PHP工作是:

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

,但我还没有使用过所以不知道如何工作的。