<?php
include_once("/var/www/html/lib/mhlib/logfile.class.php");
include_once("/var/www/html/openlib/pest/Pest.php");

date_default_timezone_set("Asia/Taipei");
$today = date("Ymd");   //20140122
$op_logfile    = "/tmp/user_msg/send_a_message/".$today.".log";

$log_file = new LogFile(LOG_ERR);
$log_file->set_pid(posix_getpid());
$log_file->set_op_log_filename($op_logfile);

if (isset($_SERVER["REMOTE_ADDR"]) && isset($_SERVER["REMOTE_PORT"])) {
    $log_file->output_log(LOG_INFO, "Remote IP: ".$_SERVER["REMOTE_ADDR"].":".$_SERVER["REMOTE_PORT"]);
}

$http_raw_post_data = file_get_contents('php://input');
$log_file->output_log(LOG_INFO, "http_raw_post_data=".$http_raw_post_data);

$pest = new Pest('https://eip.ws.hgiga/');
$pest->curl_opts[CURLOPT_TIMEOUT] = 10;
try {
    $eip_response = $pest->post('/user_msg/send_a_message/', $http_raw_post_data);
    $log_file->output_log(LOG_INFO, "eip_response=".$eip_response);
    echo $eip_response;
}
catch (Exception $e)
{
     /* 經過實驗如果發生timeout，則會產生exception */
    $error_msg = "Caught exception: " . $e->getMessage();
    $log_file->output_log(LOG_INFO, "error_msg=".$error_msg);
    $response = '{"ERROR_CODE":"500", "ERROR_MESSAGE":"Internal Server Error!"}';
    $log_file->output_log(LOG_INFO, "response=".$response);
    echo $response;
}
?>