我如何在 Joomla 中使用 HttpFactory?

How can I use HttpFactory in Joomla?

我在 Joomla 中使用了 curl,

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'X-API-KEY:' . $api_key,
        'X-SANDBOX:' . $sandbox,
));

但现在我使用 HttpFactory 请求 api:

$options = array(
    'Content-Type: application/json',
    'X-API-KEY:' . $api_key,
    'X-SANDBOX:' . $sandbox,
 );
 $answer = $this->http->post($url,json_encode($data,true),$options);

我的结果正文是:

Unsupported request content type application/x-www-form-urlencoded"

为什么?

您的 $options 数组必须是如下所示的关联数组:

$options = array(
    'Content-Type' => 'application/json',
    'X-API-KEY' => $api_key,
    'X-SANDBOX' => $sandbox,
);

简短说明:Http class 中 post() 方法的第三个参数需要特定格式的 $headers 信息:

An array of name-value pairs to include in the header of the request.

Link to Joomla Documentation

您可以从 Joomla Stack Exchange 的许多专家那里获得 Joomla 帮助:https://joomla.stackexchange.com/