3

I'm new to Google Documents and have set up a spreadsheet that accesses the amount of "Likes" on three different Facebook pages. 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. 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:

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. 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). Is this a conflict between Javascript and 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. Anyways, any help is greatly appreciated. Thanks.

9
  • So, what is your problem? After invoking document.write you get nothing on your page? Try using firebug for firefox to see if javascript throws any error - if it does, post it. Commented Feb 17, 2012 at 8:50
  • Yeah, nothing appears on the screen when I invoke document.write . I've used Firefox and see no problems. I did just try to use alert() to display the number and it didn't work. I replaced the number with a simple "Hello" alert and it worked fine though... Commented Feb 17, 2012 at 14:44
  • put some console.log('some simple text') inside FacebookFans function and check the result in firebug. maybe it will help us somehow. Commented Feb 17, 2012 at 14:47
  • you can also provide some more info about invoking your javascript - when (event maybe?) you want to use it and where it is defined Commented Feb 17, 2012 at 14:48
  • Let me use that console.log and get back to you. Eventually I would like to invoke the javascript in a 'badge' of sorts on the site where it displays the total number of fans across the different Facebook pages I manage. At this point, I'm just trying to get it to do a simple print out before I move forward. I've created this on a blank page with nothing else right now so I know there won't be any conflicts with other code before I enter it into a page. Commented Feb 17, 2012 at 14:53

2 Answers 2

1
+50

I used the Google Docs script debugger to step through your script.

The first arrow is what starts the script. In the dropdown for Select Function, choose the "doit" function I created (see bottom of screenshot). Finally, you can click where the second arrow is pointing and create a breakpoint, so the debugger stops.

google docs script debugger

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.

You need certain "Actions" to run your scripts. You can read more about how scripts are run in Google docs. But your document.write doesn't apply here because you aren't writing a script for a webpage. You are inside the Google Docs environment.

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. If you load that script (and put it inside tags) in a .html doc, you can use Google Chrome to find out the errors. Select Wrench Icon->Tools->Javascript Console and it will show you the error right away. 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).

To translate this into server side code, it's pretty simple in PHP. You are basically just calling one url and then parsing it with XML. To load the contents of the url, use file_get_contents and then parse the xml.

Sign up to request clarification or add additional context in comments.

3 Comments

Right right. What I am trying to do is take this script and write it in a webpage (which I'm building with PHP). The variable that is being retrieved (number of Facebook Fans) is what I am trying to retrieve for the webpage using document.write. I'm assuming now that there is some action within the script that applies exclusively to Google Docs and is preventing this from running in basic javascript on a website?
Oh so I don't even need the Javascript at all? I just need to take that URL and enter in the "aPageId" into the appropriate place and then parse the fan_count from the xml you're saying? I think that makes sense, although I feel like it's much simpler than I thought it would be (that would be great if it was!).
Perfect, you actually lead me right where I needed to go. Here's what I did: I set the URL in PHP as a $facebook_info . I used "$xml = simplexml_load_file($facebook_info);" Then "$fan_count = $xml->page->fan_count;" (which is how the XML is organized through the API). Then "echo $fan_count". Thank you so much. I now have flexibility to use the fan counts as I need!
0

If FacebookFans(40796308305) really works and returns result, so the problem is somehow in 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.

3 Comments

I tried the alert as you posted it and it didn't work. I then simply tried to alert("Hello!") to test and make sure it wasn't something else around it and the alert worked fine. I'm assume it has something to do with the function now?
Yes, the problem is in function. I use step-by-step debug in chrome dev.tool to solve such problems.
The script actually works in other forms (that's what I'm having trouble figuring out). It was designed for Google Docs and you can use the code to get results in a spreadsheet in Google Docs. So I'm having trouble figuring out why it won't work in a traditional webpage setting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.