コード例 #1
0
 public static function sendToServer($postdata = array(), $stat = false)
 {
     if (count($postdata) > 0) {
         if ($stat) {
             if ($counter_id = get_option(_PREFIX_STAT . 'counter_id')) {
                 $postdata['counter_id'] = $counter_id;
             }
         }
         $postdata = http_build_query($postdata, '', '&');
         $length = strlen($postdata);
         if (function_exists("curl_init") && function_exists("curl_setopt") && function_exists("curl_exec") && function_exists("curl_close")) {
             if ($stat) {
                 $url = SERVER_URL_VISIT_STAT . "/Api.php";
             } else {
                 $url = WPADM_URL_BASE . "api/";
             }
             $curl = curl_init($url);
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($curl, CURLOPT_POST, true);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
             self::$result = curl_exec($curl);
             curl_close($curl);
             if ($stat) {
                 return unserialize(self::$result);
             } else {
                 return json_decode(self::$result, true);
             }
         } elseif (function_exists("fsockopen")) {
             if ($stat) {
                 $url = SERVER_URL_STAT;
                 $req = '/Api.php';
             } else {
                 $url = substr(WPADM_URL_BASE, 7);
                 $req = '/api/';
             }
             $out = "POST " . $req . " HTTP/1.1\r\n";
             $out .= "HOST: " . $url . "\r\n";
             $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
             $out .= "Content-Length: " . $length . "\r\n";
             $out .= "Connection:Close\r\n\r\n";
             $out .= $postdata . "\r\n\r\n";
             try {
                 $errno = '';
                 $errstr = '';
                 $socket = @fsockopen($url, 80, $errno, $errstr, 30);
                 if ($socket) {
                     if (!fwrite($socket, $out)) {
                         throw new Exception("unable to write fsockopen");
                     } else {
                         while ($in = @fgets($socket, 1024)) {
                             self::$result .= $in;
                         }
                     }
                     self::$result = explode("\r\n\r\n", self::$result);
                     if ($stat) {
                         return unserialize(self::$result);
                     } else {
                         return json_decode(self::$result, true);
                     }
                     throw new Exception("error in data");
                 } else {
                     throw new Exception("unable to create socket");
                 }
                 fclose($socket);
             } catch (exception $e) {
                 return false;
             }
         }
     }
 }