1

I use last version of wordpress and woocommerce but I get this {"code":"woocommerce_rest_cannot_view","message":"Sorry, you cannot list resources.","data":{"status":401}} when I try to get product by curl in php. there is my code:

$ch = curl_init("http://localhost/wp-json/wc/v1/products  -u ck_4e2f7166b48212888b1bb1d0a6e54371878abfd1:cs_822687fc36460c6a6d767737c967dfcfdcf2c3db");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
echo $result;
curl_close($ch);

I also use \ before -u but still I get Error 401. is there any spacial option should I use for CURL in php?

1 Answer 1

2

The curl_init function accepts an url and what you are passing there looks like a command line call.

Please check this link for an example of passing username and password through php curl functionality.

Basically you have to use just http://localhost/wp-json/wc/v1/products in curl_init and set username using curl_setopt function with CURLOPT_USERPWD parameter.

Example:

$ch = curl_init('http://localhost/wp-json/wc/v1/products');
curl_setopt($ch, CURLOPT_USERPWD,'your credentials');
//other options here
$res = curl_exec($ch);
curl_close($ch);
12
  • It didn't work.I get that CURL from this doc woocommerce-rest-api-docs
    – saeed Am
    Commented Sep 3, 2016 at 12:01
  • That example is for command line curl. Are you sure you have right permissions for the user you are trying to use? Commented Sep 3, 2016 at 17:01
  • My user is admin and i checked token
    – saeed Am
    Commented Sep 4, 2016 at 6:54
  • You need to authenticate the user before making any request. As suggested by WooCommerce team, you should use already implemented library for making requests. Commented Sep 5, 2016 at 7:19
  • The only thing which you need to do is to go to command line(unix) and type in this command composer require automattic/woocommerce . It will install their library. Alternatively, you can go to woocommerce api docs page, Introduction -> Libraries and Tools and follow the link to download PHP library Commented Sep 5, 2016 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.