在 wordpress 中解码来自 API 的 JSON 响应

纳菲

我正在尝试解码来自 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;
纳菲

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章