0

I'm building a very simple API in PHP to give me data from my DB in application/json format.

Here is my muscular-groups.php file :

<?php

include '../inc/db.php';

if (isset($_GET['id'])) {
    $req = $pdo->prepare('SELECT * FROM muscular_groups WHERE id = ?');
    $req->execute([$_GET['id']]);
    $res = $req->fetchAll();
}
else {
    $req = $pdo->prepare('SELECT * FROM muscular_groups');
    $req->execute();
    $res = $req->fetchAll();
}

header('Content-Type: application/json');
echo json_encode($res);

The problem is that in local all works well as you can see below : enter image description here

But when I want to access it on my server (at www.example.com/api/muscular-groups), I have a white screen without any error message or error logs.

What is strange : If I replace header('Content-Type: application/json'); and echo json_encode($res); with var_dump($res) in muscular-groups.php, it appears on the screen.

P.S.: very strange, only one of my endpoints works on my server (eg: www.example.com/api/offers) and displays a json output, here's the code :

<?php

include '../inc/db.php';

$req = $pdo->prepare('SELECT * FROM offers');
$req->execute();
$res = $req->fetchAll();

header('Content-Type: application/json');
echo json_encode($res);

I'm lost...

Maxime Lafarie
  • 2,172
  • 1
  • 22
  • 41
  • First question: What version of PHP is running on your server that does not work? – RiggsFolly Jul 11 '16 at 15:27
  • Suggestion: Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while running on the server that does not work_ right after your opening PHP tag for example ` – RiggsFolly Jul 11 '16 at 15:27
  • @RiggsFolly my php version is 5.6.17-0+deb8u1. I already tried it, but nothing appears. Also in my php-fpm php.ini file but it seems to be already enabled. – Maxime Lafarie Jul 11 '16 at 15:29
  • @RiggsFolly I tried your suggestion anyway, but nothing happened too :/ – Maxime Lafarie Jul 11 '16 at 15:33

1 Answers1

0

I finally got it : the problem was there was UTF-8 characters, and PDO was not configured to give them as-is. So $pdo->exec("SET NAMES 'utf8'"); saved my life!

Maxime Lafarie
  • 2,172
  • 1
  • 22
  • 41