我正在尝试从API获取数据,为此我使用的是php curl。
我的API数据如下所示:
{"COLUMNS":["COCD","CONAME","KINDOFACCOUNT","ACCOUNTCODE","ACCOUNTNAME","TELNO","FAX","ADDR","OPENINGBALANCE","DR_AMT","CR_AMT","VOUCHERDATE","SETTLEMENT_NO","CTRCODE","CTRNAME","TRANS_TYPE","VOUCHERNO","NARRATION","BILLNO","CHQNO","EXPECTED_DATE","TRADING_COCD","PANNO","EMAIL","MANUALVNO","BOOKTYPECODE","BILL_DATE","MKT_TYPE","GROUPCODE","BRSFLAG","SETL_PAYINDATE","LAST2SETL","ACCOUNTCODE1","GATEWAYID","PUNCH_TIME","TRANS_TYPE1"],"DATA":[["MCX","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["MCX","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["NSE_FNO","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["NSE_FNO","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["NSE_FNO","","PARTY","TT3405","ANTHONIRAJ P","","","","0","0.00","500.00","","0","0","","OP","0","OPENING BALANCE","0","0","","NSE_FNO","","","0","3","","","","","","","TT3405","","",""],["MCX","","PARTY","TT3405","ANTHONIRAJ P","","","","0","0.00","1192.77","","0","0","","OP","0","OPENING BALANCE","0","0","","MCX","","","0","3","","","","","","","TT3405","","",""]]}
我能够获取数组,即所有COLUMNS名称,但当我尝试仅获取COCD列时,它说:注意:尝试获取非对象的属性
我的代码如下:
<?php
$url = 'http://myapi.cfm';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$characters = json_decode($response, true);
print_r ($characters['COLUMNS']);
foreach ($characters as $character) {
print_r ($character->CONAME . '<br>');
}
}
?>
请帮助,我现在很长时间都在寻找答案。
答案 0 :(得分:0)
以下是翻译此数据的一种方法......
// Standard JSON decode, no `true` second parameter
$source = json_decode('{"COLUMNS":["COCD","CONAME","KINDOFACCOUNT","ACCOUNTCODE","ACCOUNTNAME","TELNO","FAX","ADDR","OPENINGBALANCE","DR_AMT","CR_AMT","VOUCHERDATE","SETTLEMENT_NO","CTRCODE","CTRNAME","TRANS_TYPE","VOUCHERNO","NARRATION","BILLNO","CHQNO","EXPECTED_DATE","TRADING_COCD","PANNO","EMAIL","MANUALVNO","BOOKTYPECODE","BILL_DATE","MKT_TYPE","GROUPCODE","BRSFLAG","SETL_PAYINDATE","LAST2SETL","ACCOUNTCODE1","GATEWAYID","PUNCH_TIME","TRANS_TYPE1"],"DATA":[["MCX","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["MCX","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["NSE_FNO","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["NSE_FNO","","PARTY","TT3405","ANTHONIRAJ P","","","","0","","","","","","","","","","","","","","","","","3","","","","","","","TT3405","","",""],["NSE_FNO","","PARTY","TT3405","ANTHONIRAJ P","","","","0","0.00","500.00","","0","0","","OP","0","OPENING BALANCE","0","0","","NSE_FNO","","","0","3","","","","","","","TT3405","","",""],["MCX","","PARTY","TT3405","ANTHONIRAJ P","","","","0","0.00","1192.77","","0","0","","OP","0","OPENING BALANCE","0","0","","MCX","","","0","3","","","","","","","TT3405","","",""]]}');
// Loop over each row and turn it into assoc array
foreach( $source->DATA as &$data )
{
$data = array_combine( $source->COLUMNS, $data );
}
// Get just the COCD column
print_R( array_column( $source->DATA, 'COCD' ) );
// Array
// (
// [0] => MCX
// [1] => MCX
// [2] => NSE_FNO
// [3] => NSE_FNO
// [4] => NSE_FNO
// [5] => MCX
// )