1
I’m trying to get data through an API (bet365) it returns this data in JSON, I created a function to extract this data and transform into array
It follows function: public Function api_football(){
$url = "https://api.betsapi.com/v1/bet365/inplay_filter?sport_id=1&token=token&LNG_ID=22" . $this->refcode;
$request_headers = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
if (curl_errno($ch))
{
print "Error: " . curl_error($ch);
}
else
{
// Show me the result
$transaction = json_decode($data, TRUE);
curl_close($ch);
//var_dump($transaction);
$dados = array();
$dados['transaction'] = $transaction;
}
$this->load->view('header');
$this->load->view('home', $dados);
$this->load->view('footer');
}
JSON template I’m working on:
{
"success": 1,
"pager": {
"page": 1,
"per_page": 1000,
"total": 4
},
"results": [
{
"id": "77564080",
"time": "1543528800",
"time_status": "1",
"league": {
"id": "3024",
"name": "Copa Libertadores - Feminino"
},
"home": {
"id": "9105",
"name": "EC Iranduba - Feminino"
},
"away": {
"id": "170148",
"name": "Atlético Huila - Feminino"
},
"ss": "1-0",
"our_event_id": "1093051"
},
I am showing the data in the view as follows:
echo ($transaction['results'][0]['league']['name']);
echo ($transaction['results'][0]['ss']);
I need to take this data without indicating which Index, I have tried the following ways:
foreach ($transaction as $row){
echo $row->name;
}
//ou
foreach ($transaction as $row){
echo $row->results['name'];
}
// ou
foreach ($transaction as $row){
echo $row->results['league']['name'];
}
Note: I want without the index because I do not want to put the games manually that is automatically placed, example: I have 10 games in the afternoon the way I am doing I have to put 10 indexes but the night may be only 2 for example.
Have you ever tried to do:
foreach($transaction['results'] as $row)
?– André Minoro Fusioka
Already! it gives the following message: trying to get Property 'Results' of non-object
– Eduarda Rosa
You can give a
var_dump($transaction)
orvar_dump($transaction->results)
? If yes edit your question with the information– André Minoro Fusioka
It comes as NULL
– Eduarda Rosa