Using the official PHP library for the WooCommerce REST API with basic auth

If a WordPress installation is protected via .htaccess basic auth, the requests against the WooCommerce REST API with the PHP library do not work. I did not find the GitHub issue where someone said that CURL could be used to set the login credentials, but with that information, I found a question and the answers on Stack Overflow, that helped me:

$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERPWD, 'user:password' );Code language: PHP (php)

I inserted these two lines at the beginning of my script and replaced user and password with the actual login data. With that, the requests against the protected installation worked.

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)