Just follow the docs: https://packagist.org/packages/automattic/woocommerce
Example:
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$url = 'http://example.com';
$consumerKey = 'ck_xxxxxxxxxxxx'; // Change for your consumer key
$consumerSecret = 'cs_xxxxxxxxxxxx'; // Change for your consumer secret
$options = [];
$api = new Client($url, $consumerKey, $consumerSecret, $options);
try {
$results = $woocommerce->get('products');
return json_encode($results);
} catch (HttpClientException $e) {
return json_encode(['error' => ['code'=> [$e->getResponse()->getCode()], 'message'=> $e->getMessage()]]);
}
Hi Claudio, thank you so much for your reply, I have started from scratch again and followed the docs for what I can understand from it.
I have successfully installed the library via composer and I have created a file products.php
in which I have added your code with the relative correct URL, key and secret.
I have uploaded all the files in the root on the server and when I go to http://example.com/products.php I get a fatal error:
Fatal error: Call to a member function get() on a non-object in /home/wardrob0/public_html/products.php on line 15
on line 15 in products.php I have
$results = $woocommerce->get('products');
Is there anything wrong with that line? I can’t figure it out from the documentation.
Thank you
Or should I put the files and folders somewhere else?
Thank you
I have solved it, sorry. It was my bad I didn’t notice the different variables.
In case someone else needs this, the correct code is this
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$url = 'http://example.com';
$consumerKey = 'ck_xxxxxxxxxxxx'; // Change for your consumer key
$consumerSecret = 'cs_xxxxxxxxxxxx'; // Change for your consumer secret
$options = [];
$woocommerce = new Client($url, $consumerKey, $consumerSecret, $options);
try {
$results = $woocommerce->get('products');
return json_encode($results);
} catch (HttpClientException $e) {
return json_encode(['error' => ['code'=> [$e->getResponse()->getCode()], 'message'=> $e->getMessage()]]);
}
I have followed the docs but I do still get the same error.
I have uploaded all the files in the root on the server, is this the correct place where I should put the files?
root
– vendor
– products.php (in this file I have added the example code you have sent me)
I need to get the products via the URL but when I go to http://example.com/wc-api/v3/products I get the error
{“errors”:[{“code”:”woocommerce_api_authentication_error”,”message”:”oauth_consumer_key parameter is missing”}]}
What am I doing wrong? Do I need to place the files somewhere else? Do I need to add something to the file products.php?
Many thanks
Jeff
Hey,
Did you solve it?? Let me know also even i am confused where to upload the library files and setup.
Thanks,