gpt4 book ai didi

php - 使用 wp_remote_post 时出现 415 错误 "Unsupported Media Type"

转载 作者:行者123 更新时间:2023-12-04 03:50:55 27 4
gpt4 key购买 nike

我尝试从 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 文件...),但没有任何效果。请帮忙,我被卡住了

最佳答案

摘自KeyCDN :

A 415 Unsupported Media Type error occurs when the origin serverrefuses a particular request since the resource is in a format that is not supported by the server for the HTTPmethodused. This unsupported format type issue can be caused by what isdefined in the resource's Content-Type or Content-Encodingheaders.


在您的情况下,错误很可能是因为您的远程请求发送了错误的 Content-Type header — 默认为 application/x-www-form-urlencoded当 HTTP 方法为 POST 时。
是的,您确实包含了正确的 Content-Type您的值(value) $headers大批。但不幸的是在您的 $arg您传递给 wp_remote_post() 的数组, 你使用了错误的数组键 — header ,实际上应该是 headers (注意“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 );

关于php - 使用 wp_remote_post 时出现 415 错误 "Unsupported Media Type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64414760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com