本文目录导读:

我将为您详细介绍PHP后台发送数据的几种常见方式:
使用 cURL 发送HTTP请求
GET 请求
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo $response; ?>
POST 请求
<?php
$data = array(
'name' => '张三',
'email' => '[email protected]'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/submit");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
?>
JSON数据发送
<?php
$data = array(
'user' => 'admin',
'password' => '123456'
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/login");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
?>
使用 file_get_contents() 发送请求
GET 请求
<?php $url = "https://api.example.com/data?name=张三&age=25"; $response = file_get_contents($url); echo $response; ?>
POST 请求(需要 stream_context_create)
<?php
$data = array(
'username' => 'admin',
'password' => '123456'
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents('https://api.example.com/login', false, $context);
echo $result;
?>
发送邮件
<?php
$to = '[email protected]';
$subject = '测试邮件';
$message = '这是一封测试邮件';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
发送到数据库
<?php
// MySQLi 示例
$mysqli = new mysqli("localhost", "username", "password", "database");
$name = "张三";
$email = "[email protected]";
$stmt = $mysqli->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);
$stmt->execute();
$stmt->close();
$mysqli->close();
// PDO 示例
try {
$pdo = new PDO("mysql:host=localhost;dbname=database", "username", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
$stmt->execute([':name' => $name, ':email' => $email]);
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
发送文件下载
<?php
$file = 'path/to/file.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
使用 Guzzle HTTP 客户端(推荐)
首先安装 Guzzle:
composer require guzzlehttp/guzzle
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
// GET 请求
$response = $client->request('GET', 'https://api.example.com/data');
echo $response->getBody();
// POST 请求
$response = $client->request('POST', 'https://api.example.com/submit', [
'form_params' => [
'name' => '张三',
'email' => '[email protected]'
]
]);
// JSON 请求
$response = $client->request('POST', 'https://api.example.com/api', [
'json' => [
'username' => 'admin',
'password' => '123456'
],
'headers' => [
'Authorization' => 'Bearer your-token'
]
]);
?>
选择建议
- 简单请求:使用
file_get_contents() - 复杂请求:使用 cURL
- 大型项目:推荐 Guzzle HTTP 客户端
- 需要异步处理:考虑使用 PHP 的
pcntl_fork()或消息队列
记得处理可能出现的错误和异常,确保数据传输的安全性。