-1

I used Php Slim Framework for my API. I install the Slim Framework to my web root directory on my server and copy the index.php file I coded.

Index.php:

<?php
require 'vendor/autoload.php';

$app = new \Slim\Slim();
$app->contentType('application/json');
$app->get('/users', 'getUsers');
$app->get('/user/:id', 'getUser');
$app->run();

function getConnection() {
$dbhost="localhost";
$dbuser="";
$dbpass="";
$dbname="";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}

function getUsers() {
$sql = "select * FROM manga";
try {
$db = getConnection();
$stmt = $db->query($sql);
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($users);
}
catch(PDOException $e) {
echo json_encode($e->getMessage());
}
}
?>

I am getting 500 (Internal Server Error).

Edit: I changed "$app = new Slim();" to the "$app = new \Slim\Slim();" then receive the below error.

I am using EasyEngine(Nginx).

enter image description here

Edit-2:Now 500 Internal gone but another error showing.

XMLHttpRequest cannot load http://api.mangayurdu.com/users. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://deneme.mangayurdu.com' is therefore not allowed access.

Here is my code that getting JSON data:

 .factory('MY', function($http){
  var factory = {};
  var url = 'http://api.mangayurdu.com/users?callback=JSON_CALLBACK';
  factory.isimler = $http.get(url);
  return factory;
  })
Nasuh
  • 355
  • 3
  • 20
  • Have you checked your server (nginx, Apache, ...) logs? What do you see there? Please edit your question to include more details about the error. – Gustavo Straube Oct 06 '15 at 22:40
  • BTW have you also uploaded the `vendor` directory? Reading your question again it seems you only uploaded a single `index.php` file. – Gustavo Straube Oct 06 '15 at 22:51
  • @Gustavo Straube I install the Slim to my server then only copy the index.php i wrote it. Vendor file its already there. I edit my question. – Nasuh Oct 06 '15 at 23:00
  • From your edit, you're hitting the [same origin policy](https://en.wikipedia.org/wiki/Same-origin_policy). Look into CORS. – ceejayoz Oct 07 '15 at 00:36

2 Answers2

3

From the posted code it looks like Slim can't find a function called getUser. getUsers() is defined in your code, but no getUser() function.

Dan Abrey
  • 696
  • 5
  • 9
  • Yeah I remove the empty function and 500 Internal gone. But still blank page. – Nasuh Oct 06 '15 at 23:55
  • The domain of your executed page and requested domain name for the HTTP request must be the same, for security reasons. See http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Dan Abrey Oct 07 '15 at 11:20
0

try putting this in the start of your PHP page

<?php header('Access-Control-Allow-Origin: *');?>
Anurag Verma
  • 485
  • 2
  • 12