2

I'm trying to setup a proper cURL call in WordPress so am using wp_remote_post(). However, I'm having trouble authenticating the user via wp_remote_post(). Any idea how to convert the following to be used in wp_remote_post?

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

Full example of proper cURL basic auth is here.

1

Use the Authorization header. Example:

$auth = base64_encode( $username . ':' . $password );

$args = [
    'headers' => [
        'Authorization' => "Basic $auth"
    ],
    'body'    => $body,
];      

$response      = wp_remote_post( $url, $args );
$response_body = wp_remote_retrieve_body( $response );
  • Great, thank you! Still not working for me, but this seems to be the right direction. I'm guessing it's something with my other header parameters. – Jeremiah Prummer Oct 14 '15 at 15:47
  • @JeremiahPrummer Start with the most basic headers, then add more until it breaks or works. :) I had a similar issue recently where the Content-Type header lead to an authorization error … some APIs are not nice. – fuxia Oct 14 '15 at 17:26
  • And yeah, I'm the first tester of this API so it needs some work... Either way, got it working thanks to you. This was extremely helpful – Jeremiah Prummer Oct 14 '15 at 17:40

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.