Hi,
I have a PHP which should return json output. I am using the same code in other application and it works fine but now getting:
headers already sent
This is my code.. Kindly help..
<?php
header("Content-Type: application/json");
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
$mysql_host = "mysql:host=mysql.mydomain.com;dbname=myDB";
$mysql_user = "mydomainuser";
$mysql_password = "mydomainPassword";
$mysql_options = array
(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
// $mysql_connection;
$mysql_connection = new PDO($mysql_host, $mysql_user, $mysql_password, $mysql_options);
$mysql_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$mysql_query = $mysql_connection->prepare('CALL sp_populate_countries()');
$mysql_query->execute();
if ($mysql_query->rowCount() <= 0) { echo "false"; }
else
{
$jsonData = '{ "Countries" :[';
while($mysql_row = $mysql_query->fetch())
{
$jsonData .= '{"country_code_alpha2":"' . $mysql_row["country_code_alpha2"] . '","country_name":"' . $mysql_row["country_name"] . '"},';
}
$jsonData = chop($jsonData, ",");
$jsonData .= ']}';
echo $jsonData;
}
?>