【问题标题】:Decode JSON response from API in wordpress在 wordpress 中解码来自 API 的 JSON 响应
【发布时间】:2020-01-24 03:23:53
【问题描述】:

我正在尝试解码来自 API 的 JSON 响应并将“serial_no”作为字符串存储在我的代码下方,但我无法获得预期的结果。我做错了什么

来自 API 的 JSON 响应

 [
        {
            "id": 20063,
            "transaction_date": "2020-01-24 10:41:031",
            "reference_id": "91",
            "voucher": [
                {
                    "serial_no": "KFCYQOqW23012024"
                }
            ]
        }
    ] 

PHP 代码

/* posting parameters to API */
$post = wp_remote_post( $url, array(
        'headers'     => array( 'Content-Type' => 'application/x-www-form-urlencoded'),
        'body' =>      $reserve_array 
    ) );
/*Decode JSON response to store in $serial */

 $result =  json_decode($post);
 $serial=$result[0]->voucher[0]->serial_no;

【问题讨论】:

  • $result[0]->voucher[0]->serial_no?
  • @dibery 我试过了,但是 $serial 为空,PHP 代码更新了 FYR

标签: php json wordpress api


【解决方案1】:

下面的PHP代码解决了我的问题

 $result =  json_decode( wp_remote_retrieve_body( $post) );
 $serial=$result[0]->voucher[0]->serial_no;

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 2017-06-14
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多