function addMessage($name, $relation, $content) { $array = array("name" => $name, "relation" => $relation, "content" => $content, "time" => date("Y-m-d H:i:s", time())); $encodeJson = json_encode($array); $decodeJson = json_decode($encodeJson); giant_lock(); $filedata = getMessage("message.json"); $json = json_decode($filedata); if ($json == null) { $json = array(); } array_push($json, $decodeJson); $fp = fopen("message.json", "w"); fwrite($fp, json_encode($json)); fclose($fp); giant_unlock(); }
function addMessage($author, $relationship, $msg) { $array = array("author" => $author, "relationship" => $relationship, "msg" => $msg, "time" => date("Y-m-d H:i:s", time())); $encodeJson = json_encode($array); $decodeJson = json_decode($encodeJson); giant_lock(); $filedata = getMessage("message.json"); $json = json_decode($filedata); if ($json == null) { $json = array(); } array_unshift($json, $decodeJson); $fp = fopen("message.json", "w"); fwrite($fp, json_encode($json)); fclose($fp); giant_unlock(); return $array; }
function curl_read($url, $timeout_ms = 5000) { giant_unlock(); // curl is slow, so unlock giant lock before cUrl try { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); // cross curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout_ms); curl_setopt($ch, CURLOPT_HEADER, 0); $data = curl_exec($ch); if (!curl_errno($ch)) { curl_close($ch); } else { $error = curl_error($ch); curl_close($ch); throw new Exception($error); } giant_lock(); return $data; } catch (Exception $ex) { giant_lock(); throw $ex; } }