【问题标题】:How do i get order notes using PHP with the woocommerce REST API json response如何使用带有 woocommerce REST API json 响应的 PHP 获取订单备注
【发布时间】:2021-03-18 20:45:09
【问题描述】:

我正在尝试关注 http://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-order-notes 从我的订单中获取订单备注,并在嵌套的 json 响应中获取作者和 ID,但它不起作用

这里有什么明显的错误吗?

require_once 'auth.php';

$notes = $woocommerce->get('orders/108668/notes');
$json = json_decode($notes, true);

foreach($json as $elem)  {
   echo($elem['id']. ", ".$elem['author'] );
   echo("<br/>");
}

如果我 print_r($notes); 我得到这样的回应

Array ( [0] =&gt; stdClass Object ( [id] =&gt; 24721 [author] =&gt; haspden [date_created] =&gt; 2020-12-07T09:03:01 [date_c........

如果我 print_r($json); 我什么都得不到:/

有什么想法吗?我确信这很明显,并且由于我对带有 json 的数组和对象之间的误解。

谢谢

【问题讨论】:

    标签: php arrays json api woocommerce


    【解决方案1】:

    尝试使用array 强制转换$notes 并使用json_encode 将其公开为json:

    $notes = $woocommerce->get('orders/108668/notes');
    header('Content-Type: application/json'); // this line is to make sure the output format is JSON
    echo json_encode((array)$notes);
    

    【讨论】:

      【解决方案2】:

      据我所知,$notes = $woocommerce-&gt;get('orders/108668/notes'); 的响应已经是一个数组,您可以遍历它并做任何您喜欢的事情。您不需要对其进行 json_decode。

      此外,json_decode 第一个参数是docs 所说的字符串,但您提供了一个数组。

      【讨论】:

        猜你喜欢
        • 2017-04-09
        • 2022-01-08
        • 2021-07-31
        • 2017-04-16
        • 2021-07-03
        • 2019-09-30
        • 2021-05-02
        • 2017-09-13
        • 1970-01-01
        相关资源
        最近更新 更多