3

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?

1

Somehow, by just adding $headers to the post call, it now works, so I took out $http->setOption(CURLOPT_HTTPHEADER, $headers); and changed the code to the following:

if (JHttpTransportCurl::isSupported()) {

    $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);
    $response = $http->post($url, $app_xml, $headers);

    # Carry on with saving reference if correct HTTP code is returned
    if ($response->code == 200 || $response->code == 100) {
        # Do something here, it was a successful call
    }
}
0

In general, Joomla requires a session token to accept a form post. Thus, a first request must get the token and submit it along the rest of the fields.

  • I don't understand how this will assist in the cURL request going through correctly. The system I'm submitting to will not use the token and might decline my application straight-away. – Anriëtte Myburgh Jul 2 '15 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.