Try using cURL in PHP:
http://php.net/manual/en/book.curl.php
There are wrappers for this, like:
http://semlabs.co.uk/journal/object-oriented-curl-class-with-multi-threading
Use options such as:
EDIT: More specific, not tested
Download the class from:
http://semlabs.co.uk/journal/object-oriented-curl-class-with-multi-threading
require_once( 'CURL.php' ); //Change this to whatever that class is called in the above
$curl = new CURL();
$curl->retry = 2;
$opts = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Linux Mint/8 (Helena) Firefox/3.5.3',
CURLOPT_COOKIEFILE => 'fb.tmp',
CURLOPT_COOKIEJAR => 'fb.tmp',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_TIMEOUT => 20
);
$post_data = array( ); //put your login POST data here
$opts[CURLOPT_POSTFIELDS] = http_build_query( $post_data );
$curl->addSession( 'https://www.facebook.com/messages', $opts );
$result = $curl->exec();
$curl->clear();
print_r( $result );
Note, that sometimes you need to load a page first, to set a cookie, before they will let you login.