public function post_request($method, $params)
{
$params['method'] = $method;
$params['session_key'] = $this->session_key;
$params['api_key'] = $this->api_key;
$params['call_id'] = microtime(true);
if ($params['call_id'] <= $this->last_call_id) {
$params['call_id'] = $this->last_call_id + 0.001;
}
$this->last_call_id = $params['call_id'];
if (!isset($params['v'])) {
$params['v'] = '1.0';
}
$post_params = array();
foreach ($params as $key => &$val) {
if (is_array($val)) {
$val = implode(',', $val);
}
$post_params[] = $key . '=' . urlencode($val);
}
$secret = $this->secret;
$post_params[] = 'sig=' . Facebook::generate_sig($params, $secret);
$post_string = implode('&', $post_params);
if (function_exists('curl_init')) {
// Use CURL if installed...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->server_addr);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion());
$result = curl_exec($ch);
curl_close($ch);
} else {
// Non-CURL based version...
$context = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" . 'User-Agent: Facebook API PHP5 Client 1.1 (non-curl) ' . phpversion() . "\r\n" . 'Content-length: ' . strlen($post_string), 'content' => $post_string));
$contextid = stream_context_create($context);
$sock = fopen($this->server_addr, 'r', false, $contextid);
if ($sock) {
$result = '';
while (!feof($sock)) {
$result .= fgets($sock, 4096);
}
fclose($sock);
}
}
return $result;
}