{"id":4563,"date":"2018-03-31T12:34:24","date_gmt":"2018-03-31T10:34:24","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4563"},"modified":"2020-02-09T11:24:49","modified_gmt":"2020-02-09T10:24:49","slug":"official-php-library-woocommerce-rest-api-basic-auth","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/official-php-library-woocommerce-rest-api-basic-auth-4563\/","title":{"rendered":"Using the official PHP library for the WooCommerce REST API with basic auth"},"content":{"rendered":"\n<p>If a WordPress installation is protected via <code class=\"lang-bash\">.htaccess<\/code> 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 <a href=\"https:\/\/stackoverflow.com\/questions\/23809392\/what-does-phps-curlopt-userpwd-do\">question and the answers on Stack Overflow<\/a>, that helped me:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ch = curl_init();\ncurl_setopt( $ch, CURLOPT_USERPWD, <span class=\"hljs-string\">'user:password'<\/span> );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I inserted these two lines at the beginning of my script and replaced <code class=\"lang-php\">user<\/code> and <code class=\"lang-php\">password<\/code> with the actual login data. With that, the requests against the protected installation worked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-4563","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":null,"server":null,"url":null},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=4563"}],"version-history":[{"count":4,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4563\/revisions"}],"predecessor-version":[{"id":5968,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4563\/revisions\/5968"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}