如何在php中检索Curl发送的数据

问题描述 投票:0回答:1

我有一个通过curl 将请求发送到end-point.php 的php 代码。在 end-point.php 中,我尝试显示传输数据的值,但没有显示任何内容。这是我的代码:

// Prepare subscriber data
$data = array(
    $name = "John",
);

// Set cURL options
$url = "https://www.example.com/end-point.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: multipart/form-data'
));

// Execute cURL request
$response = curl_exec($ch);
$curl_error = curl_error($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Check if the request was successful
if ($http_code >= 200 && $http_code < 300) {
    echo $response;
} else {
    // Output error message
    echo "Error";
}

这是 end-point.php 文件的代码:

echo $_POST['name'];
php curl post
1个回答
0
投票

我不知道这个语法是否有效,但是

$data
有错误应该是:

// Prepare subscriber data
$data = array(
    name => "John",
);
© www.soinside.com 2019 - 2024. All rights reserved.