I would like to perform a cURL request using the API of Joomla 3.4 using the POST method. I have following code, but keep getting a 401 error.
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode($username . ":" . $pass),
'X-Auth-Token' => urlencode($auth_token),
'Content-Length' => urlencode(strlen($app_xml)),
);
$options = new Joomla\Registry\Registry();
$http = JHttpFactory::getHttp($options);
$http->setOption(CURLOPT_POST, 1);
$http->setOption(CURLOPT_POSTFIELDS, $app_xml);
$http->setOption(CURLOPT_RETURNTRANSFER, 1);
$http->setOption(CURLOPT_HTTPHEADER, $headers);
$response = $http->post($url, $app_xml);
Using the following vanilla cURL request, it works and gives back a 200-response:
$ch = curl_init();
$headers = array(
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode($username . ":" . $pass),
'X-Auth-Token: ' . urlencode($auth_token),
'Content-Length: ' . urlencode(strlen($app_xml)),
);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $app_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//execute post
$result = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//close connection
curl_close($ch);
Anything I'm doing wrong here in the Joomla-part of my code?