我正在尝试使用 POST 将一些 json 格式的数据发送到特定的 URL test.php。请求应该是异步的,我需要获取 http 响应代码来判断数据是否已发送(我不想等到请求在 test.php 上处理完毕)。
我已经建立了一个 perl 函数来发送数据
#!/usr/bin/perl
use strict;
use warnings;
use JSON qw(encode_json);
use AnyEvent;
use AnyEvent::HTTP;
=pod
Function send asynchronus data , get response code
=cut
sub sendOutboundPostRequestAsync {
my ($url, $data, $token) = @_;
# Create a condition variable to wait for the asynchronous request
my $request_done = AnyEvent->condvar;
# Convert the data hash to JSON
my $json_data = encode_json($data);
print "JSON data being sent: $json_data\n"; # For debugging
# Make the HTTP POST request asynchronously
http_post $url,
headers => {
'Authorization' => $token,
'Content-Type' => 'application/json',
},
body => $json_data,
sub {
my ($body, $headers) = @_;
my $status_code = $headers->{Status} // 500; # Default to 500 if no status
# Print the response for debugging
print "Response Body: $body\n";
print "Request completed with status code: $status_code\n";
# Send the status code back through the condition variable
$request_done->send($status_code);
};
return $request_done; # Return the condition variable for further processing
}
#####
my $url = 'http://xxx.xxx.xxx/test.php';
my $token = 'eyJ0eXAiOiJKV1QiLCJhbGci';
# Example data to send
my %data = (
uniqueid => '1727331196.gesti',
start_time => '2024-09-26 08:13:17',
);
# Call the function
my $response_code_condvar = sendOutboundPostRequestAsync($url, \%data, $token);
my $response_code = $response_code_condvar->recv; # Wait for the request to complete
print "Final Response Code: $response_code\n";
获取数据的 php 代码(为了测试目的而缩短)
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Retrieve the raw POST data
$postData = file_get_contents('php://input');
// Log all headers for debugging
$headers = getallheaders();
file_put_contents('postData.txt', "Received Headers:\n" . print_r($headers, true) . "\n", FILE_APPEND);
// Specify the file path
$file_path = 'postData.txt';
// Open the file for writing (append mode)
$file = fopen($file_path, 'a'); // Use 'w' to overwrite the file
// Write the POST data to the file
fwrite($file, "Received POST data:\n" . $postData . "\n\n");
// Close the file
fclose($file);
// Decode the JSON data
$data = json_decode($postData, true);
// Check if data decoding was successful
if ($data === null) {
// Handle error: Invalid JSON
http_response_code(400);
echo 'Invalid JSON';
exit;
}
// Extract the action from the decoded data
$action = $data['action'] ?? null;
// Check if the action is set
if ($action === null) {
// Handle error: Missing action
http_response_code(400);
echo 'Missing action';
exit;
}
?>
问题
执行 Perl 脚本后我得到
ast1:~ # perl test.agi
JSON data being sent: {"start_time":"2024-09-26 08:13:17","uniqueid":"1727331196.gesti"}
Response Body: Invalid JSON
Request completed with status code: 400
Final Response Code: 400
数据似乎正确,应该可以毫无问题地发送,但在 PHP 端的文本日志中,我得到了
Received POST data:
headers
期望输出
Received POST data:
{
"start_time": "2024-09-26 08:13:17",
"uniqueid": "1727331196.gesti"
}
问题
- 这是使用 Perl 发送异步数据的最佳方式吗?
- 数据结果为空可能是什么问题?