Ich habe versucht, eine POST-Anfrage von WordPress an eine externe API zu senden (einem Benutzer in einem CRM-System ein Tag zuzuweisen). Als ich cURL benutzte, war alles in Ordnung. Hier ist der cURL-Code:
function my_function () {
$body = array ( 'tags' => array ( array (
'email' => '[email protected]',
'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');
Aber dann bin ich auf Nutzung umgestiegen wp_remote_post
habe ich die Antwort „415 – Nicht unterstützter Medientyp“ erhalten.
$body = array ( 'tags' => array (
array(
'email' => '[email protected]',
'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>';
Ich habe viele Modifikationen ausprobiert (assoziatives Array-Format in Schlüssel:Wert-Paar geändert, add AddType
zu htaccess
Datei…), aber nichts hat funktioniert. Bitte helft mir, ich stecke fest