Reputation: 1009
I want to use the Woocommerce REST API to get json data using PHP. How will i get that?
The cURL code is :
curl https://example.com/wc-api/v3/products \
-u consumer_key:consumer_secret
Node.js returns json but php api isn't? Is there php api to return json data?
Find documentation for Woocommerce API here
Upvotes: 2
Views: 5086
Reputation: 375
For PHP, You got to use WooCommerce REST API PHP Client Library
Here is an example
$products = array();
try {
$client = new WC_API_Client( store_url, consumer_key, consumer_secret, $options );
if (isset($_POST['category_id'])) {
$category_id = $_POST['category_id'];
$products = $client->products->get_categories($category_id);
}
} catch ( WC_API_Client_Exception $e ) {
$error_msg = $e->getMessage() . PHP_EOL;
// echo $e->getCode() . PHP_EOL;
}
$response = array();
if ($products) {
$response['error'] = false;
$response['data'] = $products;
} else {
$response['error'] = true;
$response['message'] = $error_msg;
}
echo json_encode($response);
Upvotes: 0
Reputation: 1060
After getting data in the form of PHP Arrays just convert it into JSON:
$result = json_encode($returned_array);
Upvotes: 3