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

Symfony4向Laravel9 POST数组ID数据接收为NULL问题求助

问题描述

我有一个遗留Symfony4 Web应用,需要向Laravel9 Web应用POST一组ID数组,但Laravel端始终无法接收到数据,返回值为NULL。用Postman测试时可以正常接收,但直接从Symfony4发送数据时,无论是无Curl方式还是Curl方式都无法传递数据(优先希望解决无Curl方式的问题,因为服务器可能未安装php-curl扩展)。


无Curl方式代码

Symfony发送端(postDataController.php)

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @route("/posting/test")
 */
class Post2WebhookController extends AbstractController
{
    /**
     * @route("/post2webhook", methods={"GET"}, name="post2webhook")
     * @Security("is_granted('ROLE_1') or is_granted('ROLE_2') or is_granted('ROLE_3')", message="error message.")
     * @return Response
     */
    public function post2webhook(){
        $ids = array(1,2,3,4,5,6);
        $url = "http://myLaravel9App.local/api/webhook";

        try{
            $result = $this->post($url,$ids);
            var_dump($result);
        }
        catch(\Exception $e){
            echo '<p style="color: red;">Not connected. More details:  </p>'.$e->getMessage();
        }

        return new Response('success');
    }

    public function post($url, $postVars = array()){
        $postStr = http_build_query($postVars);
        $options = array(
            'http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postStr
                )
        );
        $streamContext  = stream_context_create($options);
        $result = file_get_contents($url, false, $streamContext);
        if($result === false){
            $error = error_get_last();
            throw new \Exception('POST request failed: ' . $error['message']);
        }
        return $result;
    }
}

Laravel接收端(webHookController.php)

<?php

namespace App\Http\Controllers\Constancias\Log;

use App\Http\Controllers\Controller;
use App\Notifications\Efirma\Constancias\Firmadas;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

class ConstanciaFirmadaController extends Controller
{
    public function constancia_firmada(Request $request){
        $ids= $request->get('content');// 始终返回NULL
        $tipo_contenido = gettype($request->get('content'));
        Notification::route('mail',
                ['funcionario@email.net'=>'Mr. John Doe']
            )
            ->notify(new Firmadas(content: $tipo_contenido))
        ;
    }
}

问题分析与修复方案

核心问题

Symfony发送端直接传递索引数组$ids,http_build_query会生成类似0=1&1=2的参数键值对,但Laravel端一直在读取content键对应的值,自然返回NULL——这和你用Postman测试时的参数结构不匹配。

无Curl方式修复

1. 修改Symfony发送端参数结构

将ID数组包装到content键下,确保参数结构和Postman测试一致:

public function post2webhook(){
    $ids = array(1,2,3,4,5,6);
    $url = "http://myLaravel9App.local/api/webhook";

    try{
        // 将ID数组包装到content键中
        $postData = ['content' => $ids];
        $result = $this->post($url, $postData);
        var_dump($result);
    }
    catch(\Exception $e){
        echo '<p style="color: red;">Not connected. More details:  </p>'.$e->getMessage();
    }

    return new Response('success');
}

2. 完善请求头(可选但推荐)

在post方法中补充Content-Length头,确保请求完整性:

public function post($url, $postVars = array()){
    $postStr = http_build_query($postVars);
    $options = array(
        'http' =>
            array(
                'method'  => 'POST',
                'header'  => implode("\r\n", [
                    'Content-type: application/x-www-form-urlencoded',
                    'Content-Length: ' . strlen($postStr)
                ]),
                'content' => $postStr
            )
    );
    $streamContext  = stream_context_create($options);
    $result = file_get_contents($url, false, $streamContext);
    if($result === false){
        $error = error_get_last();
        throw new \Exception('POST request failed: ' . $error['message']);
    }
    return $result;
}

3. Laravel端验证

临时添加打印代码确认参数是否到达:

public function constancia_firmada(Request $request){
    // 先打印所有请求参数,确认数据结构
    dd($request->all());
    $ids= $request->get('content');
    $tipo_contenido = gettype($ids);
    Notification::route('mail',
            ['funcionario@email.net'=>'Mr. John Doe']
        )
        ->notify(new Firmadas(content: $tipo_contenido))
    ;
}

Curl方式修复(非优先)

如果后续需要启用Curl,确保参数结构和请求头正确:

public function post2webhook(){
    $ids = array(1,2,3,4,5,6);
    $url = "http://myLaravel9App.local/api/webhook";
    $postData = ['content' => $ids];
    $postStr = http_build_query($postData);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($postStr)
    ]);

    $result = curl_exec($ch);
    if(curl_errno($ch)){
        throw new \Exception('Curl request failed: ' . curl_error($ch));
    }
    curl_close($ch);
    var_dump($result);
    return new Response('success');
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:01:01