How to Capture This Array

Asked

Viewed 36 times

0

I’m trying to make a API.

This 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 API with the code below, but does not return me anything:

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

$json = json_decode($captura);

var_dump($json);

1 answer

3

It is necessary that you print the content 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 variable $row in the array $jsonObj, but he’s not responsible for the 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 serve to transform the array $jsonObj for a string in format json and the echo will be to print this string onscreen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.