首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP fsockopen 實作 GET/POST 表單提交與檔案上傳完整範例

     編輯:PHP 綜合知識

前言

使用 PHP 底層 fsockopen 手動組裝 HTTP 請求,能深入理解協議細節,適合無 cURL 環境或教學用途。本文提供 GET、POST(含檔案上傳)三種完整可執行範例。

1. GET 請求

client_get.php

<?php
$host = 'demo.fdipzone.com';
$port = 80;
$errno = $errstr = '';
$timeout = 30;
$url = '/socket/getapi.php';

$param = ['name' => 'fdipzone', 'gender' => 'man'];
$url .= '?' . http_build_query($param);

$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) die('Connection failed');

$out = "GET $url HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: close\r\n\r\n";

fputs($fp, $out);
$response = '';
while ($row = fread($fp, 4096)) $response .= $row;
fclose($fp);

$pos = strpos($response, "\r\n\r\n");
echo substr($response, $pos + 4);
?>

server_getapi.php

<?php
$name = $_GET['name'];
$gender = $_GET['gender'];
echo "name=$name<br>";
echo "gender=$gender";
?>

2. POST 表單 (application/x-www-form-urlencoded)

client_post.php

<?php
$host = 'demo.fdipzone.com';
$port = 80;
$url = '/socket/postapi.php';
$param = ['name' => 'fdipzone', 'gender' => 'man'];
$body = http_build_query($param);

$fp = fsockopen($host, $port, $errno, $errstr, 30);
$out = "POST $url HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-Length: " . strlen($body) . "\r\n";
$out .= "Connection: close\r\n\r\n$body";

fputs($fp, $out);
$response = '';
while ($row = fread($fp, 4096)) $response .= $row;
fclose($fp);
$pos = strpos($response, "\r\n\r\n");
echo substr($response, $pos + 4);
?>

3. 檔案上傳

client_upload.php

<?php
$host = 'demo.fdipzone.com';
$port = 80;
$url = '/socket/uploadapi.php';
$boundary = '----Boundary' . md5(microtime());

$filePath = '/path/to/test.jpg';
$fileName = 'test.jpg';
$fileType = 'image/jpeg';
$fileContent = file_get_contents($filePath);

$body = "--$boundary\r\n";
$body .= "Content-Disposition: form-data; name=\"file\"; filename=\"$fileName\"\r\n";
$body .= "Content-Type: $fileType\r\n\r\n";
$body .= $fileContent . "\r\n";
$body .= "--$boundary--\r\n";

$fp = fsockopen($host, $port, $errno, $errstr, 30);
$out = "POST $url HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Content-Type: multipart/form-data; boundary=$boundary\r\n";
$out .= "Content-Length: " . strlen($body) . "\r\n";
$out .= "Connection: close\r\n\r\n$body";

fputs($fp, $out);
$response = '';
while ($row = fread($fp, 4096)) $response .= $row;
fclose($fp);
$pos = strpos($response, "\r\n\r\n");
echo substr($response, $pos + 4);
?>

server_uploadapi.php

<?php
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    echo "Upload success";
} else {
    echo "Upload failed: " . $_FILES['file']['error'];
}
?>

關鍵點總結

  • 請求行:METHOD URL HTTP/1.1
  • Host 標頭:HTTP/1.1 強制要求。
  • Content-Length:POST/Multipart 必須準確計算 Body 長度。
  • 雙 CRLF 分隔:標頭結束以 \r\n\r\n 分隔 Body。
  • 回應解析:尋找 \r\n\r\n 後為實際內容。
  • Multipart 邊界:以 --boundary 開頭,--boundary-- 結尾,每部分含 Content-Disposition、Content-Type。

結語

手寫 Socket HTTP 客戶端是理解 Web 通訊本質的最佳練習。實務開發請改用 cURL 或 Guzzle,享受 HTTPS、重試、並發、流式處理等企業級能力。本範例代碼已精簡至核心邏輯,可直接複製測試。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved