Wooccommerce的cURL api在php中获取json


Woocommerce cURL api to get json in php

我想使用Wooccommerce REST API来使用PHP获取json数据。我怎么才能拿到?

cURL代码为:

curl https://example.com/wc-api/v3/products '
-u consumer_key:consumer_secret

Node.js返回json,而php-api不是?有php api返回json数据吗?

在此处查找Wooccommerce API的文档

以PHP数组的形式获取数据后,只需将其转换为JSON:

$result = json_encode($returned_array);

对于PHP,您必须使用WooCommerce REST API PHP客户端库

以下是的示例

$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);