How can I use CURLOPT_USERPWD in wp_remote_post?

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 Answer
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 );

Leave a Comment