Farmville

Farmville is a flash-based Facebook game created and owned by Zynga Inc. Currently it is available in most Flash-supported browsers from either http://apps.facebook.com/onthefarm/ and http://www.farmville.com/

Farmville communicates with Farmville servers using the |AMF (Action Message Format) via HTTP POSTs to http://fb-0.farmville.com/flashservices/gateway.php.

Farmville servers use AMFPHP, and AMFPHP can also be used to send messages to Farmville via PHP rather than by the offical Flash Client.

To do so, you require certain values which you must include each of your Farmville requests, such as:
 * Facebook User ID
 * Farmville Token (given in sourcecode of http://fb-0.farmville.com/flash.php - requires cookies)
 * The Current Time (formatted with two decimal places, however it need not be that exact)
 * The number of the most recent Official Client Flash Revision (also available in the sourcecode of the page specified above)

Then you can create a request for the function UserService.initUser which gives you all information about the current state of the user's farm.

In PHP with earlier versions of AMFPHP (the version I tried from their website did NOT work), you can create a request with:

// Include AMFPHP Core files define('AMFPHP_BASE', './amfphp/core/'); require_once(AMFPHP_BASE . "shared/util/CharsetHandler.php"); require_once(AMFPHP_BASE . "amf/util/AMFObject.php"); require_once(AMFPHP_BASE . "shared/util/CompatPhp5.php"); require_once(AMFPHP_BASE . "shared/util/MessageBody.php"); require_once(AMFPHP_BASE . "amf/io/AMFSerializer.php"); require_once(AMFPHP_BASE . "amf/io/AMFDeserializer.php");

// Generate payload $payload = array(					array( 'userId' => 'USER_ID_GOES_HERE', 'token' => 'TOKEN_GOES_HERE', 'sigTime' => floatval(time.'.03'), 'flashRevision' => FLASHREVISION_GOES_HERE, ), 				array( array(								'params' => array( "", -660),								'sequence' => 1,								'functionName' => 'UserService.initUser'						) ),				(float) 0			); // Create the AMF object for the request $amf = new AMFObject; $amf->addBody(new MessageBody('BaseService.dispatchBatch','/1',$payload));

// Serialize the AMF object into a string we can send to the server $serializer = new AMFSerializer; $result = $serializer->serialize($amf);

// POST it using cURL $ch = curl_init('http://fb-0.farmville.com/flashservices/gateway.php'); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_TIMEOUT,20); curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-amf')); curl_setopt($ch,CURLOPT_POSTFIELDS,$result); $result = curl_exec($ch);

Please expand this wiki article by contributing to it.