【发布时间】: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 文件...),但没有任何效果。请帮忙,我卡住了
【问题讨论】: