php 不依赖第三方服务与 java 后台服务通信

服务端 java 监听端口

package com.st.xk;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class XKServer {

	private static boolean isDo = true;

	public static void main(String[] args) {
		try {
			ServerSocket ss = new ServerSocket(8090, 10, InetAddress.getByName("172.16.2.8"));
			while (isDo) {
				Socket s = ss.accept();
				new WorkerThread(s).start();
			}
			ss.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Runtime.getRuntime().addShutdownHook(new Thread(){
			public void run(){
				isDo = false;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
	}

}
package com.st.xk;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class WorkerThread extends Thread {

	private Socket socket;

	public WorkerThread(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			
			StringBuilder sb = new StringBuilder();
			String str;
			while((str = reader.readLine()) != null){
				sb.append(str);
			}
			socket.shutdownInput();
			
			writer.write(sb.toString());
			writer.flush();
			socket.shutdownOutput();
			
			reader.close();
			writer.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

客户端 php 利用 socket 连接向 java 程序发送消息

< ?php
function testSocket($i){
	$str = '{id:'.$i.',time:'.time().'}';
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	socket_connect($socket, '172.16.2.8', 8090);
	socket_write($socket, $str, strlen($str));
	socket_shutdown($socket, 1);
	while($rStr = socket_read($socket, 4096)){
		echo $rStr;
	}
	echo "\n";
	socket_close($socket);
}
?>

利用 DNSPod 开放API 实现域名动态解析 PHP版

vim ddns.php

< ?php
$domain_id  = 3441736;
$sub_domain = 'blog';
$record_id  = 23626557;
function ddns_curl($api, $data=array()){
        $data['login_email'] = '登录邮箱';
        $data['login_password'] = '登录密码';
        $data['format'] = 'json';
        $data['lang'] = 'en';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://dnsapi.cn/'.$api);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_USERAGENT, 'DNSPod API PHP Web Client/0.1 (chinanou.com@gmail.com)');
        $response = curl_exec($ch);
        curl_close($ch);
        return json_decode($response);
}
function ddns_log($msg){
        echo date('Y-m-d H:i:s')," {\n",var_dump($msg),"}\n";
}
$res = ddns_curl('Record.List', array( 'domain_id' => $domain_id, 'sub_domain' => $sub_domain ));
if($res->status->code != 1){
        ddns_log($res);
}else{
        $record = $res->records[0];
        $currentIp = file_get_contents('http://t.nt00.com/ip.php');
        ddns_log($record->value.' > '.$currentIp);
        if($record->value != $currentIp){
                $res = ddns_curl('Record.Modify', array(
                        'domain_id' => $domain_id,
                        'record_id' => $record_id,
                        'sub_domain' => $sub_domain,
                        'record_type' => 'A',
                        'record_line' => '默认',
                        'value' => $currentIp,
                        'ttl' => 120
                ));
                ddns_log($res);
        }
}

#cronetab -e
*/2 * * * * /xxx/bin/php -f /xxx/ddns.php >> ddns.log