我正在创建一个使用 etsy api 来显示商店信息的网站。我正在尝试访问重新解析的 json 字符串的各个值。为此,我需要将返回的字符串转换为对象。我能看到的唯一方法是json_decode($response_body);
,但我似乎无法让它工作。当我使用该函数时,当我尝试获取它的类型时它返回 undefined/NULL。返回的字符串看起来像这样:{"count":3,"results":[{"listing_id":252525252,"state":"active","user_id":1111111,"category_id":1234567,"title":"Title of product","description":"This is a description"}
. 我做错了什么吗?这是我正在使用的代码:
$url = "https://api.etsy.com/v2/shops/shopname/listings/active?api_key=".$apikey;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (intval($status) != 200) throw new Exception("HTTP $status\n$response_body");
$reponse_fixed = json_decode($response_body);
echo $response_fixed;