1

我尝试使用CURL如下方式发出 HTTP 请求:

$rawQuery = '{
    "CUSTNAME" : "1970188",
    "CURDATE":"2020-12-28T00:00:00+02:00",
    "BOOKNUM":"Test BookNum",
    "DETAILS":"Test Details"
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://somelink.co.de");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $rawQuery); 
$curl_response = curl_exec($ch);

这个正在返回我的结果。但是当我尝试使用 Symfony HTTP 客户端实现相同的功能时,我收到 400 错误。

这是代码,我试过了。

$response = $this->client->request('POST', $url, [
    'auth_basic' => [
        'username',
        'password'
    ],
    'headers' => [
        'Accept' => 'application/json', 
    ],
    'json' => $rawQuery
]);

我不确定我缺少什么Client

有人可以帮我吗?

4

1 回答 1

0

您的原始查询不正确。这应该是一个 php 数组,当您在请求中使用“json”键时,Symfony 正在尝试为您进行 json_encode()。将您的 $rawQuery 重新格式化为

$rawQuery = [
    "CUSTNAME" => "1970188",
    "CURDATE"  => "2020-12-28T00:00:00+02:00",
    "BOOKNUM"  => "Test BookNum",
    "DETAILS"  => "Test Details"
];
于 2021-03-01T14:33:25.720 回答