简体   繁体   English

在PHP中调用Javascript函数不起作用

[英]Calling Javascript Function In PHP Not Working

I'm new to Google Documents and have set up a spreadsheet that accesses the amount of "Likes" on three different Facebook pages. 我是Google文档的新手,并设置了一个电子表格,可以访问三个不同Facebook页面上的“喜欢”数量。 The code is part of the library on Google Documents but I'm trying to take the resulting total and pull it up on my site which is PHP. 该代码是Google Documents上的库的一部分,但我正在尝试将得到的总数并将其提取到我的网站上,即PHP。 I'm starting simply with one site just because I can't get it working. 我只是因为无法让它工作而开始只使用一个网站。

Here is the Javascript that was written to compile the likes: 这是为编译喜欢而编写的Javascript:

function FacebookFans(aPageId)
{
  if (aPageId === undefined || aPageId === null)
  {
    throw "No parameter specified. Write Facebook PageID as parameter."
  }

  if (typeof aPageId != "number")
    throw "Parameter must be number.";

  // See http://developers.facebook.com/docs/reference/fql/page/ for API documentation
  var url = "http://api.facebook.com/method/fql.query?query=SELECT%20page_id,page_url,fan_count%20FROM%20page%20%20WHERE%20page_id=%22" + encodeURIComponent(aPageId) + "%22";

  var response = UrlFetchApp.fetch(url);

  if (response.getResponseCode() != 200)
    throw "Unexpected response code from Facebook.";

  var responseText = response.getContentText();

  if (responseText == null || responseText == "")
    throw "Empty response from Facebook.";

  var fan_count = 0;

  try
  {
    var xml = Xml.parse(responseText, false);
    var page = xml.getElement().getElement();

    if (page == null)
      throw "Wrong PageID.";

    fan_count = parseInt(page.getElement("fan_count").getText());
  }
  catch (e)
  {
    throw "Problem with response from Facebook: " + e;
  }

  return fan_count;
}

Now, to preface, I am very new at Javascript so don't kill me if my code is way off, I'm still trying to understand. 现在,作为序言,我是Javascript的新手,所以如果我的代码已经关闭,不要杀了我,我仍然试图理解。 I tried to run this in the body: 我试图在体内运行:

<script type="text/javascript">
document.write(FacebookFans(40796308305));
</script>

I figured the function returns a value and this would print that value out (the number btw is Coca Cola's Facebook page ID, figured it was a good one to test with). 我认为该函数返回一个值,这将打印出该值(btw的数字是可口可乐的Facebook页面ID,认为它是一个很好的测试)。 Is this a conflict between Javascript and PHP? 这是Javascript和PHP之间的冲突吗? I know that's a mixture of client-side and server side scripts. 我知道这是客户端和服务器端脚本的混合体。 The reason I'm not sure what's wrong though is that I set a var inside the Javascript and then used to document.write to call it back just to test that my code was valid and it recalled the var fine. 我不确定什么是错的原因是我在Javascript中设置了一个var ,然后用于document.write调用它来测试我的代码是否有效并且它调用了var Anyways, any help is greatly appreciated. 无论如何,非常感谢任何帮助。 Thanks. 谢谢。

I used the Google Docs script debugger to step through your script. 我使用Google Docs脚本调试程序逐步完成您的脚本。

The first arrow is what starts the script. 第一个箭头是启动脚本的原因。 In the dropdown for Select Function, choose the "doit" function I created (see bottom of screenshot). 在Select Function的下拉列表中,选择我创建的“doit”功能(参见屏幕截图的底部)。 Finally, you can click where the second arrow is pointing and create a breakpoint, so the debugger stops. 最后,您可以单击第二个箭头指向的位置并创建断点,以便调试器停止。

谷歌文档脚本调试器

After stepping through all of your code, you'll be glad to know it works just fine. 单步完成所有代码后,您会很高兴知道它的工作正常。

Your problem must be related to your understanding of how/when the code will be run. 您的问题必须与您对代码运行方式/时间的理解有关。 Note there is NO PHP in any of this code, so I'm not sure why you were asking about PHP. 注意在这些代码中没有PHP,所以我不确定你为什么要问PHP。

You need certain "Actions" to run your scripts. 您需要某些“操作”才能运行脚本。 You can read more about how scripts are run in Google docs . 您可以阅读有关如何在Google文档中运行脚本的详细信息。 But your document.write doesn't apply here because you aren't writing a script for a webpage. 但是你的document.write不适用于此,因为你没有为网页编写脚本。 You are inside the Google Docs environment. 您位于Google文档环境中。

If you want to run your script outside of Google Docs, you have a problem with the UrlFetchApp call, since that is a Google specific thing. 如果您想 Google文档之外运行脚本,则UrlFetchApp调用会出现问题,因为这是特定于Google的内容。 If you load that script (and put it inside tags) in a .html doc, you can use Google Chrome to find out the errors. 如果您在.html文档中加载该脚本(并将其放入标记内),则可以使用Google Chrome查找错误。 Select Wrench Icon->Tools->Javascript Console and it will show you the error right away. 选择扳手图标 - >工具 - > Javascript控制台,它会立即显示错误。 Now, normally you could just translate this to something else, but Javascript does its best to prevent you from making cross domain requests ( learn more ). 现在,通常您可以将其转换为其他内容,但Javascript会尽力阻止您发出跨域请求( 了解详情 )。

To translate this into server side code, it's pretty simple in PHP. 要将其转换为服务器端代码,它在PHP中非常简单。 You are basically just calling one url and then parsing it with XML. 您基本上只是调用一个URL然后使用XML解析它。 To load the contents of the url, use file_get_contents and then parse the xml . 要加载url的内容,请使用file_get_contents然后解析xml

If FacebookFans(40796308305) really works and returns result, so the problem is somehow in document.write . 如果FacebookFans(40796308305)真的有效并返回结果,那么问题就是在document.write Try: 尝试:

 <script type="text/javascript">
 alert(FacebookFans(40796308305));
 </script>

To be sure that the function works. 确保该功能正常工作。 And: 和:

 <script type="text/javascript">
 var a=FacebookFans(40796308305);
 document.write('a='+a);
 </script>

To check the different types of issues. 检查不同类型的问题。

If nothing helps, so we need more info, how do you invoke this function. 如果没有任何帮助,那么我们需要更多信息,你如何调用这个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM