wp_remote_request header error even though working properly with cURL
I'm trying to make calls to a JSON API inside my wordpress theme. I tried with the cURL option, making a custom function and things works as it should but I wanted to use the built-in wordpress functions and with the same parameters I keep getting an error "Invalid request header"
here are my parameters:
$payload = array
(
'httpversion' = '1.0',
'headers' = array
(
'Content-type' = 'application/json',
'Accept' = 'application/json; charset=utf-8',
'Authorization' = 'Basic ' . base64_encode( $login . ':' . $password )
),
'timeout' = 0,
'body' = $params,
'method' = 'POST'
);
the $params object has all the params I have to pass to the API, I'm not sure it is relevant since it's working with the custom function
and I'm calling the wp_remote_request as follow:
$response = wp_remote_request ( 'https://the/api/url', $payload);
var_dump($response);
the cURL function that is working is the following one:
function do_curl($username,$password,$url, $params){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-type: application/json","Accept: application/json; charset=utf-8"));
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
$jsonResponse = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status != 200) {
//IF ERROR
//Get http-Body (if aplicable) from CURL-response
$body = json_decode(curl_multi_getcontent($curl), true);
//Build array, containing the body (Response data, like Error-messages etc.) and the http-status-code
$response = array(
"status" = $status . " | " . curl_error($curl),
"body" = $body
);
} else {
//IF OK
//Convert response into an Array
$body = json_decode($jsonResponse, true);
//Build array, containing the body (Response-data) and the http-status-code
$response = array(
"status" = $status,
"body" = $body
);
}
//IMPORTANT!!!
//Close connection!
curl_close($curl);
//$response, again, is a multi-dimensional Array, containing the status-code ($response["status"]) and the API-response (if available) itself ($response["body"])
return $response;
}
I looked everywhere but couldn't find any reason, I tried modifying the headers in the $payload array, and even though i read a lot of things, I couldn't find anything relevant or with a solution. Maybe it's as simple as a typo
Topic wp-remote-post wp-remote-request http-api Wordpress
Category Web