-1

I've written a PHP script that reads data from Facebook user's profile and sends it to my server through AJAX. This script needs to run in intervals, so I added a cron. All the PHP functions and interactions with databases through MySQL are working with cron. But AJAX is not. I know it's (JavaScript) a client-side script, but is there any other way? How I can execute JavaScript (AJAX) written in a file through cron.

Dharman
  • 30,962
  • 25
  • 85
  • 135
new_user
  • 1
  • 3
  • What have you tried? Have you got the cross-domain-security restriction in using ajax? Some code please. – VKen Oct 22 '12 at 00:45

2 Answers2

1

Ajax is a client-side technology you cannot run that in PHP console.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
0

Ajax calls consist of an API (application programming interface). The API is the system of calls through which a client (the browser) communicates with a server (the server).

In your case, the client is the computer on which the PHP script is running and the server is your server. If you can replicate the API used in JavaScript in PHP then you are away on hack.

For example, suppose the sever you were communicating with had a service called "postupdate.php" and it took one value called "update", the API call might look like this in JavaScript:

<script type="text/javascript">
var response;

$.ajax({
  url: "http://www.example.com/postupdate.php?update=This_is_my_update"
}).success(function(text) { 
  response = text;
});

</script>

The same API call might look like this in PHP:

<?php

$response = file_get_contents("http://www.example.com/postupdate.php?update=This_is_my_update")

?>

This example assumes the API uses the HTTP GET method. APIs that use HTTP POST methods are more complicated but can be used in PHP as well.

Oliver Moran
  • 5,137
  • 4
  • 31
  • 45
  • Initially, I used fb php-sdk to post to facebook graph. BUt for some cases it didn't work, returning me with a #500 error. THen I turned to ajax to post the parameters with JS, and it worked on all cases. I tried posting with curl also but the same 500 error was returned to me – new_user Oct 20 '12 at 11:46
  • 500 is an internal server error (i.e. typically an error running a script on the sever). Which computer returned this? Your "client" (written in PHP) or your server? In either case, check your code. If it was Facebook that returned this, I would be surprised. But even if so, check your code again and make sure you are following their specification. – Oliver Moran Oct 20 '12 at 11:52
  • I'm receiving this error {"error":{"message":"(#500) Message contains banned content","type":"OAuthException","code":500}} on trying to comment on my wallposts. THe message that I'm sending is just random. Nothing offensive or banned. Using ajax to comment on posts works but neither curl nor the sdk functions. :-( – new_user Oct 20 '12 at 12:10
  • Check the API docs and your code just to be sure. Two things you might look out for is the loss of session data (i.e. you're not logged in properly) or issues to do with string encoding (ASCII vs. unicode). Check other API methods, particularly "read only" methods, and see if they work and debug from there. – Oliver Moran Oct 20 '12 at 17:38