You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中通过POST方法将生成的XML文件发送至指定URL?

不用表单通过POST发送XML并触发PDF下载

方法一:使用cURL发送请求

直接用cURL构造multipart/form-data请求,把生成的XML内容作为文件字段发送,同时带上表单里的generate参数,最后处理返回的PDF内容触发下载:

$created_xml = $xmlBuilder->getXML();
$order_id = "你的实际订单ID"; // 替换为真实订单ID

// 目标接口地址
$url = "https://www.szamlazz.hu/szamla/";

// 初始化cURL会话
$ch = curl_init($url);

// 构造POST数据:模拟文件上传和提交按钮参数
$postData = [
    'action-xmlagentxmlfile' => new CURLStringFile($created_xml, $order_id . ".xml", 'text/xml'),
    'generate' => 'Számla generálás'
];

// 设置cURL选项
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 生产环境不建议关闭SSL验证,仅在测试时临时使用
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

// 执行请求并获取响应
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// 关闭cURL会话
curl_close($ch);

// 判断请求是否成功,触发PDF下载
if ($httpCode === 200 && strpos($http_response_header[0], 'application/pdf') !== false) {
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="invoice.pdf"');
    header('Content-Length: ' . strlen($response));
    echo $response;
    exit;
} else {
    // 错误处理:可输出响应内容排查问题
    echo "请求失败,状态码:" . $httpCode;
    // echo $response;
}

方法二:使用file_get_contents发送请求

如果服务器开启了allow_url_fopen,也可以用这种方式构造请求:

$created_xml = $xmlBuilder->getXML();
$order_id = "你的实际订单ID";
$url = "https://www.szamlazz.hu/szamla/";

// 生成multipart分隔符
$boundary = '----WebKitFormBoundary' . uniqid();

// 构造请求体内容
$body = "--$boundary\r\n";
$body .= "Content-Disposition: form-data; name=\"action-xmlagentxmlfile\"; filename=\"{$order_id}.xml\"\r\n";
$body .= "Content-Type: text/xml\r\n\r\n";
$body .= $created_xml . "\r\n";
$body .= "--$boundary\r\n";
$body .= "Content-Disposition: form-data; name=\"generate\"\r\n\r\n";
$body .= "Számla generálás\r\n";
$body .= "--$boundary--\r\n";

// 设置请求头
$headers = [
    "Content-Type: multipart/form-data; boundary=$boundary",
    "Content-Length: " . strlen($body)
];

// 创建请求上下文
$options = [
    'http' => [
        'method' => 'POST',
        'header' => implode("\r\n", $headers),
        'content' => $body,
        'ignore_errors' => true
    ]
];
$context = stream_context_create($options);

// 发送请求并获取响应
$response = file_get_contents($url, false, $context);
$httpCode = $http_response_header[0];

// 处理响应触发下载
if (strpos($httpCode, '200 OK') !== false && strpos($http_response_header[1], 'application/pdf') !== false) {
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="invoice.pdf"');
    header('Content-Length: ' . strlen($response));
    echo $response;
    exit;
} else {
    echo "请求失败,响应状态:" . $httpCode;
}

关键注意点

  • 无需将XML写入本地文件,直接用生成的字符串发送即可,减少IO开销。
  • 生产环境务必保留SSL证书验证,不要随意关闭相关选项。
  • 如果返回的不是PDF,可临时输出响应内容,排查XML格式或参数是否符合接口要求。

内容的提问来源于stack exchange,提问作者GrappwiiyOP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 02:03:21