【问题标题】:Reading Displaying and Filtering Woocommerce JSON data from API curl request through PHP通过 PHP 从 API curl 请求中读取显示和过滤 Woocommerce JSON 数据
【发布时间】:2015-08-09 16:46:16
【问题描述】:

我对 PHP 和 API 的美妙世界还比较陌生。为了为您设置场景,我们有 12 家在线商店,全部位于不同的域中,有些位于不同的服务器上,全部本地化为本地语言等。所有这些都运行 Wordpress 和 Woocommerce 2.3.13。

我目前正在尝试制作一个“网络应用”类型的仪表板,它通过 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 文档,但似乎无法理解如何将我正在阅读的内容应用到 PHP 中的 curl 命令。

如果您需要更多信息来帮助,请尽管问,我是秃头,但如果我有头发,我会把它撕掉!

提前致谢!

【问题讨论】:

    标签: php json wordpress curl woocommerce


    【解决方案1】:

    this tutorial 看来,date_mindate_max 不是正确的过滤器。教程说这是在 2014 年 1 月获得订单的正确方法。

    $ 卷曲 https://www.skyverge.com/wc-api/v1/orders?filter[created_at_min]=2014-01-01&filter[created_at_max]=2014-01-31

    【讨论】:

    • 你是对的,在某种程度上,事实上我弄错了,但我在字符串末尾也缺少一个 & 符号,就像这样.. 'code'$chuko = curl_init( 'example.com/wc-api/v2/orders/…);无论如何,感谢您的帮助,它为我指明了正确的方向!
    【解决方案2】:

    对于任何寻找我问题答案的人,我将重新发布我正在使用的 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 数据时遇到问题,但那是另一回事了!

    【讨论】:

    • 使用您的代码并获取{"errors":[{"code":"woocommerce_api_authentication_error","message":"oauth_consumer_key parameter is missing"}]} 我该如何解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 2016-05-14
    • 2017-04-16
    • 1970-01-01
    • 2017-11-21
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多