本文目录导读:

在 PHP 中连接 Zapier 主要有两种方式,具体取决于你的需求:
使用 Webhooks(最常见)
创建 Zapier Webhook
- 在 Zapier 创建 Webhook
- 登录 Zapier,创建新 Zap
- 选择 "Webhooks by Zapier" 作为 Trigger
- 选择 "Catch Hook" 或 "Catch Raw Hook"
- 复制生成的 Webhook URL
PHP 发送数据到 Zapier
<?php
// 方法1: 使用 cURL
function sendToZapier($webhookUrl, $data) {
$ch = curl_init($webhookUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'status' => $httpCode,
'response' => $response
];
}
// 使用示例
$zapierWebhookUrl = 'https://hooks.zapier.com/hooks/catch/your_unique_id/';
$data = [
'user_id' => 123,
'email' => 'user@example.com',
'name' => 'John Doe',
'timestamp' => date('Y-m-d H:i:s')
];
$result = sendToZapier($zapierWebhookUrl, $data);
?>
<?php
// 方法2: 使用 file_get_contents(更简单)
$webhookUrl = 'https://hooks.zapier.com/hooks/catch/your_unique_id/';
$data = ['name' => 'John', 'email' => 'john@example.com'];
$options = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents($webhookUrl, false, $context);
?>
使用 Guzzle(推荐用于大型项目)
<?php
require 'vendor/autoload.php'; // 安装 Guzzle
use GuzzleHttp\Client;
function sendToZapierGuzzle($webhookUrl, $data) {
$client = new Client();
try {
$response = $client->post($webhookUrl, [
'json' => $data,
'headers' => [
'Accept' => 'application/json'
]
]);
return $response->getBody()->getContents();
} catch (\Exception $e) {
return $e->getMessage();
}
}
// 使用
$data = [
'customer' => [
'id' => 1,
'name' => 'Alice'
],
'order' => [
'id' => 100,
'total' => 250.50
]
];
$result = sendToZapierGuzzle('https://hooks.zapier.com/hooks/catch/xxx', $data);
?>
从 Zapier 接收数据到 PHP
创建 PHP 端点接收 Webhook
<?php
// receive.php
header('Content-Type: application/json');
// 获取 POST 数据
$postData = json_decode(file_get_contents('php://input'), true);
// 验证请求(可选但推荐)
$webhookSecret = 'your_secret_key';
$headers = getallheaders();
if (isset($headers['X-Zapier-Secret']) && $headers['X-Zapier-Secret'] !== $webhookSecret) {
http_response_code(403);
echo json_encode(['error' => 'Invalid secret']);
exit;
}
// 处理数据
if (!empty($postData)) {
// 在这里处理接收到的数据
file_put_contents('log.txt', json_encode($postData) . PHP_EOL, FILE_APPEND);
// 返回成功响应
http_response_code(200);
echo json_encode([
'status' => 'success',
'received' => true
]);
} else {
http_response_code(400);
echo json_encode(['error' => 'No data received']);
}
?>
配置 Zapier Webhook 到 PHP
- 在 Zapier 中选择 "Webhooks" 作为 Action
- 选择 "POST" 方法
- 输入你的 PHP 端点 URL(如:
https://yourdomain.com/receive.php) - 配置要发送的数据字段
完整示例:PHP 与 Zapier 交互
<?php
class ZapierClient {
private $webhookUrl;
private $secret;
public function __construct($webhookUrl, $secret = null) {
$this->webhookUrl = $webhookUrl;
$this->secret = $secret;
}
public function send($data, $method = 'POST') {
$ch = curl_init($this->webhookUrl);
headers = [
'Content-Type: application/json',
'Accept: application/json'
];
if ($this->secret) {
$headers[] = 'X-Zapier-Secret: ' . $this->secret;
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($error) {
throw new Exception('Curl error: ' . $error);
}
return json_decode($result, true);
}
public function receive() {
$data = json_decode(file_get_contents('php://input'), true);
// 验证请求
$headers = getallheaders();
if ($this->secret &&
(!isset($headers['X-Zapier-Secret']) ||
$headers['X-Zapier-Secret'] !== $this->secret)) {
http_response_code(401);
return ['error' => 'Unauthorized'];
}
return $data;
}
}
// 使用示例
try {
$zapier = new ZapierClient('https://hooks.zapier.com/hooks/catch/xxx', 'your_secret');
// 发送数据到 Zapier
$result = $zapier->send([
'event_type' => 'new_order',
'order_data' => [
'order_id' => 12345,
'customer_email' => 'customer@example.com',
'total_amount' => 150.00
]
]);
echo "Successfully sent to Zapier";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
?>
安装依赖(如果使用 Composer)
# 安装 Guzzle composer require guzzlehttp/guzzle # 或者使用简单的 cURL(不需要额外依赖)
重要注意事项
-
Webhook URL 安全:不要把 Webhook URL 暴露在客户端代码中
-
错误处理:始终处理可能的错误和超时
-
数据格式:Zapier 支持 JSON 格式,确保数据正确编码
-
测试:先在开发环境测试,然后部署到生产
-
异步请求:对于非关键操作,考虑异步发送(如使用队列)
// 异步发送示例(使用非阻塞请求)
function sendAsync($url, $data) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => 1, // 短超时
CURLOPT_NOSIGNAL => 1
]);
// 不等待响应
$ch = curl_exec($curl);
curl_close($curl);
}
这样你就可以在 PHP 应用和 Zapier 之间进行双向通信了!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播