【问题标题】:Got 415 error "Unsupported Media Type" when use wp_remote_post使用 wp_remote_post 时出现 415 错误“不支持的媒体类型”
【发布时间】:2023-03-13 08:40:02
【问题描述】:

我尝试从 WordPress 向外部 API 发送 POST 请求(将标签分配给 CRM 系统中的用户)。当我使用 cURL 时,一切正常。这是 cURL 代码:

function my_function () {

$body = array ( 'tags' => array ( array (
                        'email' => 'xxx@gmail.com',
                        'tag' => 'Customer5'
                        ))
);

$curl = curl_init();

curl_setopt_array($curl, array(

  CURLOPT_URL => "$api_url",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => json_encode($body, true),
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "User-Agent: Your App Name (www.yourapp.com)",
    "Authorization: Basic xxxxxx"
  ),
));

$response = curl_exec($curl);

curl_close($curl);

var_dump($response);

}

add_action( 'init', 'my_function'); 

但后来我改用wp_remote_post,我收到“415 - 不支持的媒体类型”响应。

$body = array ( 'tags' => array (
                            array(
                                'email' => 'xxx@gmail.com',
                                'tag' => 'Customer5'
                            ))
);

$headers = array (
                'Content-Type' => 'application/json',
                'User-Agent' => 'Your App Name (www.yourapp.com)',
                'Authorization' => 'Basic xxxxxx',
);

$request = wp_remote_post('$api_url', $arg );

$arg = array (
    'header' => $headers,
    'body' => json_encode($body, true),
    'method' => 'POST',
    'sslverify' => false,
);

echo '<pre>';
print_r($request);
echo '</pre>';

我尝试了很多修改(将关联数组格式更改为键:值对,将AddType 添加到htaccess 文件...),但没有任何效果。请帮忙,我卡住了

【问题讨论】:

    标签: php wordpress api curl


    【解决方案1】:

    摘自KeyCDN

    源站服务器发生415 Unsupported Media Type错误 拒绝特定请求,因为该资源的格式不受 HTTP method 的服务器支持 用过的。这种不受支持的格式类型问题可能是由 在资源的 Content-TypeContent-Encoding 中定义 标题。

    在您的情况下,错误的发生很可能是因为您的远程请求发送了错误的 Content-Type 标头 - 当 HTTP 方法为 POST 时默认为 application/x-www-form-urlencoded

    是的,您确实在 $headers 数组中包含了正确的 Content-Type 值。但不幸的是,在您传递给wp_remote_post()$arg 数组中,您使用了错误的数组键header,实际上应该是header<strong>s</strong>(注意“s”)。

    所以请使用headers 而不是header,如下所示:

    $api_url = 'your API URL';
    
    $body = array(
        'tags' => array(
            array(
                'email' => 'xxx@gmail.com',
                'tag'   => 'Customer5',
            ),
        ),
    );
    
    $headers = array(
        'Content-Type'  => 'application/json',
        'User-Agent'    => 'Your App Name (www.yourapp.com)',
        'Authorization' => 'Basic xxxxxx',
    );
    
    $arg = array(
        'headers'   => $headers, // good
    //  'header'    => $headers, // bad; i.e. wrong array key ('header')
        'body'      => json_encode( $body ),
        // 'method' can be omitted since you're using wp_remote_post()
        'method'    => 'POST',
        'sslverify' => false,
    );
    
    $request = wp_remote_post( $api_url, $arg );
    // ..if the response still isn't good, what's the output of this:
    var_dump( $request );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 2018-07-25
      • 2016-09-09
      • 1970-01-01
      • 2017-08-28
      • 2021-10-12
      • 2017-10-23
      • 2014-10-02
      相关资源
      最近更新 更多