How to Capture this array

0

I'm trying to make an API .

Here is my code:

require('conexao/conexao.php');


$jsonObj= array();



$query="SELECT * FROM filmes";
$sql = mysql_query($query)or die(mysql_error());

while($data = mysql_fetch_assoc($sql))
{ 
    $row[] = $data;
    header("Content-type:application/json");            
    array_push($jsonObj,$row);

}

I'm trying to get the data from the API with the code below, but it does not return anything:

$url = "http://fimhd.com.br/api.php";
$captura = file_get_contents($url);

$json = json_decode($captura);

var_dump($json);
    
asked by anonymous 14.02.2018 / 21:14

1 answer

3

You need to print the contents on the screen. For this you will have to use two functions: json_encode and echo .

The array_push will only serve to add the result of the $row variable in array $jsonObj , but it is not responsible for printing.

Follow the example code.

<?php

require('conexao/conexao.php');

$jsonObj= array();

$sql = mysql_query("SELECT * FROM filmes") or die(mysql_error());

while($data = mysql_fetch_assoc($sql))
{ 
    $row[] = $data;
    array_push($jsonObj,$row);
}

header("Content-type:application/json");
echo json_encode($jsonObj);

The json_encode will transform the array $jsonObj to a string in the format json and the echo will be to print this string on the screen.

    
14.02.2018 / 21:19