API credential | WordPress.org

[ad_1]

Hi, I’m testing the API but trouble with credentials.
When I hardcode the credentials inside of base64_encode(), it works fine.
However, when I save the credentials to the wp_config.php and call it to the file, it responds 401 error.

The code is like the following.

1. Import the API from wp_config.php
echo WC_SHIP_KEY;
echo WC_SHIP_VALUE;
$credentials = WC_SHIP_KEY . ':' . WC_SHIP_VALUE;
$checking = is_string($credentials);
error_log(print_r('credential: ' . $credentials . ' and ' . $checking, true)); // it is a string with the correct value.

2. The response part:
$response = wp_remote_post(
$url,
array(
'headers' => array(
‘Content-Type’ => ‘application/json’,
‘Authorization’ => ‘Basic ‘ . base64_encode($credentials),
),
‘body’ => $request_body
)
);

Thank you,

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer