从curl数组返回值

时间:2018-12-27 13:53:33

标签: php arrays api

我已经设置了curl以返回一些值,但是我不确定如何检索子值。当我尝试检索 $ bankname 时,它是空的,因为我认为它是子值。

$bin = "492182";
$url = "https://lookup.binlist.net/" . $bin;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($ch);
curl_close($ch);
$details = json_decode($resp, true);
$cardscheme = strtoupper($details['scheme']);
$cardtype = ucwords($details['type']);
$cardbrand = ucwords($details['brand']);
$bankname = strtoupper($details['bank']); //<<< need this value

下面是api:

    curl / https
    curl -H "Accept-Version: 3" "https://lookup.binlist.net/45717360"
    {
      "number": {
        "length": 16,
        "luhn": true
      },
      "scheme": "visa",
      "type": "debit",
      "brand": "Visa/Dankort",
      "prepaid": false,
      "country": {
        "numeric": "208",
        "alpha2": "DK",
        "name": "Denmark",
        "emoji": "",
        "currency": "DKK",
        "latitude": 56,
        "longitude": 10
      },
      "bank": {
        "name": "Jyske Bank",
        "url": "www.jyskebank.dk",
        "phone": "+4589893300",
        "city": "Hjørring"
      }
    }

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

作为@Sfili注释,如果您想要银行名称,请使用以下示例:

$s = '{"type":"debit", "brand": "Visa_Dankort", "bank": {"name": "Jyske Bank", "url": "www.jyskebank.dk"}}';
$details = json_decode($s, true);
echo $details["bank"]["name"];

这将输出:Jyske Bank

您无法获得此行$bankname = strtoupper($details['bank']);,因为$details['bank']是一个数组,strtoupper是一个期望的字符串。

如果我理解您的意思,请执行:$bankname = strtoupper($details['bank']['name']);