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.
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.
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.