Browse Source

Create the Contet-type header from php (#5169)

Joan Miquel 5 years ago
parent
commit
4993d3f429
2 changed files with 7 additions and 6 deletions
  1. 5 0
      frameworks/PHP/php-ngx/app.php
  2. 2 6
      frameworks/PHP/php-ngx/deploy/nginx.conf

+ 5 - 0
frameworks/PHP/php-ngx/app.php

@@ -6,6 +6,7 @@ $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser',
 function db()
 function db()
 {
 {
     global $pdo;
     global $pdo;
+    ngx_header_set('Content-Type', 'application/json');
 
 
     $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
     $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
 
 
@@ -16,6 +17,7 @@ function db()
 function query()
 function query()
 {
 {
     global $pdo;
     global $pdo;
+    ngx_header_set('Content-Type', 'application/json');
 
 
     $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
     $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
 
 
@@ -36,6 +38,8 @@ function query()
 function update()
 function update()
 {
 {
     global $pdo;
     global $pdo;
+    ngx_header_set('Content-Type', 'application/json');
+
     $query_count = 1;
     $query_count = 1;
     $params      = ngx::query_args()['queries'];
     $params      = ngx::query_args()['queries'];
     if ($params > 1) {
     if ($params > 1) {
@@ -63,6 +67,7 @@ function update()
 function fortune()
 function fortune()
 {
 {
     global $pdo;
     global $pdo;
+    ngx_header_set('Content-Type', 'text/html;charset=UTF-8');
 
 
     $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
     $fortune = $pdo->prepare('SELECT id,message FROM Fortune');
     $fortune->execute();
     $fortune->execute();

+ 2 - 6
frameworks/PHP/php-ngx/deploy/nginx.conf

@@ -37,42 +37,38 @@ http {
         php_keepalive 200;
         php_keepalive 200;
 
 
         location = /hello {
         location = /hello {
-            add_header Content-Type text/plain;
             content_by_php '
             content_by_php '
+                ngx_header_set("Content-Type", "text/plain");
                 echo "Hello, World!";
                 echo "Hello, World!";
             ';
             ';
         }
         }
 
 
         location = /json {
         location = /json {
-            add_header Content-Type application/json;
             content_by_php '
             content_by_php '
+                ngx_header_set("Content-Type", "application/json");
                 echo json_encode(["message" => "Hello, World!"]);
                 echo json_encode(["message" => "Hello, World!"]);
             ';
             ';
         }
         }
         
         
         location = /fortune {
         location = /fortune {
-            add_header Content-Type "text/html; charset=UTF-8";
 	        content_by_php '
 	        content_by_php '
                 fortune();
                 fortune();
             ';
             ';
         }
         }
 
 
         location = /db {
         location = /db {
-            add_header Content-Type application/json;
             content_by_php '
             content_by_php '
                 db();
                 db();
             ';
             ';
         }
         }
 
 
         location /query {
         location /query {
-            add_header Content-Type application/json;
             content_by_php '
             content_by_php '
                 query();
                 query();
             ';
             ';
         }
         }
 
 
         location /update {
         location /update {
-            add_header Content-Type application/json;
             content_by_php '
             content_by_php '
                 update();
                 update();
             ';
             ';