原文內容似為 Excel FV 函數(未來值)財務計算教學,與標題「PHP 程式與伺服器端通訊」不符。此處依標題重寫相關主題。
$ch = curl_init('https://api.example.com/endpoint');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(['key' => 'value']),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
$data = json_decode($response, true);
$opts = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => http_build_query(['key' => 'value']),
'timeout' => 10,
]
];
$context = stream_context_create($opts);
$response = file_get_contents('https://api.example.com/endpoint', false, $context);
$data = json_decode($response, true);
$fp = fsockopen('ssl://api.example.com', 443, $errno, $errstr, 10);
if ($fp) {
$req = "POST /endpoint HTTP/1.1\r\n";
$req .= "Host: api.example.com\r\n";
$req .= "Content-Type: application/x-www-form-urlencoded\r\n";
$req .= "Content-Length: " . strlen($body) . "\r\n";
$req .= "Connection: close\r\n\r\n";
$req .= $body;
fwrite($fp, $req);
$resp = '';
while (!feof($fp)) $resp .= fgets($fp, 1024);
fclose($fp);
// 需自行解析 HTTP 回應標頭與主體
}
使用 SoapClient 或 xmlrpc_encode_request 進行結構化 RPC 呼叫。
CURLOPT_SSL_VERIFYPEER => true)。