通过 PHP 从 API curl 请求读取显示和过滤 Woocommerce JSON 数据
Reading Displaying and Filtering Woocommerce JSON data from API curl request through PHP
我对 PHP 和 API 的精彩世界还比较陌生。为了为您设置场景,我们有 12 家在线商店,全部位于不同的域,一些位于不同的服务器,全部本地化为本地语言等。所有这些都是 运行 Wordpress 和 Woocommerce 2.3.13。
我目前正在尝试制作一个 'web app' 类型的仪表板,它通过 API 调用 Woocommerce 数据并以友好的方式从所有商店显示它。我正在使用 PHP/HTML 来显示数据,到目前为止,我已经设法毫无问题地获取了每个站点的订单数。但是现在我希望更深入地研究报告并获得一些销售数据和其他各种信息。
为了实现订单计数显示我使用了以下代码:
<?php
$data = array();
$data_string = json_encode($data);
$username = 'ck_111111111111'; // Add your own Consumer Key here
$password = 'cs_111111111111'; // Add your own Consumer Secret here
$ch = curl_init('https://example.com/wc-api/v2/orders/count/?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result = json_decode($result,true);
echo $result['count'];
curl_close($ch);
?>
这会显示该特定 woocommerce 商店的订单数。没问题。
但现在我真的很想获得销售报告,我曾尝试在 url 上添加日期过滤器,但是当将日期过滤器放在 url 的末尾时,就像这样
$ch = curl_init('https://tinywaist.co.uk/wc-api/v2/orders/count/sales?filter[date_min]=2015-01-18&filter[date_max]=2015-01-21?consumer_key='.$username.'&consumer_secret='.$password);
我什么都没有得到。
我一直在尝试对上面的代码进行各种操作来实现我需要的,我到处搜索以找到解决这个问题的方法。我真正没有掌握的是如何从 Woocommerce API 调用特定数据并将其作为数组显示在前端,订单数是我所知道的。
我已经阅读、重新阅读并再次阅读 Woocommerce REST API 文档,但似乎无法理解如何将我正在阅读的内容应用于 [=] 中的 curl 命令37=].
如果您需要更多信息来帮助,请尽管问,我是秃头,但如果我有头发,我会把它撕掉!
提前致谢!
从 this tutorial 看来 date_min
和 date_max
不是正确的过滤器。教程说这是在 2014 年 1 月获取订单的正确方法。
如果有人在寻找我的问题的答案,我将重新发布我正在使用的 CURL 请求。
关于 Woocommerce REST 的文档 API 很差而且数量不足!
$data = array();
$data_string = json_encode($data);
$username = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init('https://example.com/wc-api/v2/orders/?filter[period]=year&filter[status]=processing&consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
我现在在解析 JSON 数据时遇到问题,但那是另一回事了!
我对 PHP 和 API 的精彩世界还比较陌生。为了为您设置场景,我们有 12 家在线商店,全部位于不同的域,一些位于不同的服务器,全部本地化为本地语言等。所有这些都是 运行 Wordpress 和 Woocommerce 2.3.13。
我目前正在尝试制作一个 'web app' 类型的仪表板,它通过 API 调用 Woocommerce 数据并以友好的方式从所有商店显示它。我正在使用 PHP/HTML 来显示数据,到目前为止,我已经设法毫无问题地获取了每个站点的订单数。但是现在我希望更深入地研究报告并获得一些销售数据和其他各种信息。
为了实现订单计数显示我使用了以下代码:
<?php
$data = array();
$data_string = json_encode($data);
$username = 'ck_111111111111'; // Add your own Consumer Key here
$password = 'cs_111111111111'; // Add your own Consumer Secret here
$ch = curl_init('https://example.com/wc-api/v2/orders/count/?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result = json_decode($result,true);
echo $result['count'];
curl_close($ch);
?>
这会显示该特定 woocommerce 商店的订单数。没问题。
但现在我真的很想获得销售报告,我曾尝试在 url 上添加日期过滤器,但是当将日期过滤器放在 url 的末尾时,就像这样
$ch = curl_init('https://tinywaist.co.uk/wc-api/v2/orders/count/sales?filter[date_min]=2015-01-18&filter[date_max]=2015-01-21?consumer_key='.$username.'&consumer_secret='.$password);
我什么都没有得到。
我一直在尝试对上面的代码进行各种操作来实现我需要的,我到处搜索以找到解决这个问题的方法。我真正没有掌握的是如何从 Woocommerce API 调用特定数据并将其作为数组显示在前端,订单数是我所知道的。
我已经阅读、重新阅读并再次阅读 Woocommerce REST API 文档,但似乎无法理解如何将我正在阅读的内容应用于 [=] 中的 curl 命令37=].
如果您需要更多信息来帮助,请尽管问,我是秃头,但如果我有头发,我会把它撕掉!
提前致谢!
从 this tutorial 看来 date_min
和 date_max
不是正确的过滤器。教程说这是在 2014 年 1 月获取订单的正确方法。
如果有人在寻找我的问题的答案,我将重新发布我正在使用的 CURL 请求。
关于 Woocommerce REST 的文档 API 很差而且数量不足!
$data = array();
$data_string = json_encode($data);
$username = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init('https://example.com/wc-api/v2/orders/?filter[period]=year&filter[status]=processing&consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
我现在在解析 JSON 数据时遇到问题,但那是另一回事了!