有没有办法从下面的代码中获取 account_name 值并解析为 json

下面是我的代码,我需要从 beow API 请求中获取 account_name 的值,请仔细阅读并提出建议。我需要获得的唯一值是account_name值。请帮忙谢谢


 $curl = curl_init();

 switch ($method){

    case "POST":

       curl_setopt($curl, CURLOPT_POST, 1);

       if ($data)

          curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

       break;

    case "PUT":

       curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");

       if ($data)

          curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                              

       break;

    default:

       if ($data)

          $url = sprintf("%s?%s", $url, http_build_query($data));

 }

 // OPTIONS:

 curl_setopt($curl, CURLOPT_URL, $url);

 curl_setopt($curl, CURLOPT_HTTPHEADER, array(

  "authorization: Bearer sk_test_df78c3c2d08ea1f266b8b19524a249e77c0eaa14",

  "content-type: application/json",

 ));

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

 // EXECUTE:

 $result = curl_exec($curl);

 if(!$result){die("Connection Failure");}

 curl_close($curl);

 return $result;

}


$bank="033";

$accno=2056194452;


$get_data = callAPI('GET', 'https://api.paystack.co/bank/resolve?account_number='.$accno.'&bank_code='.urlencode($bank), false);

$response = json_decode(json_encode($get_data), true);

echo $response;````


#Result of code will output the below:#


`{ "status": true, "message": "Account number resolved", "data": { "account_number": "2056194452", "account_name": "OLADEPO WISDOM IBRAHIM", "bank_id": 18 } }`



慕村9548890
浏览 116回答 1
1回答

倚天杖

API 返回 JSON 数据,因此没有理由对其进行编码。如果您使用 json_decode 并将“assoc”参数设置为 true(就像您已经设置的那样),它应该返回一个关联数组。然后您可以从数组中获取“account_name”变量。$get_data = callAPI('GET', 'https://api.paystack.co/bank/resolve?account_number='.$accno.'&bank_code='.urlencode($bank), false);$response = json_decode($get_data, true);echo $response['data']['account_name'];
打开App,查看更多内容
随时随地看视频慕课网APP