Skip to content

Instantly share code, notes, and snippets.

@chengyuhui
Created March 31, 2013 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chengyuhui/5280459 to your computer and use it in GitHub Desktop.
Save chengyuhui/5280459 to your computer and use it in GitHub Desktop.
<?php
/**
* Step 1: Require the Slim Framework
*
* If you are not using Composer, you need to require the
* Slim Framework and register its PSR-0 autoloader.
*
* If you are using Composer, you can skip this step.
*/
require 'Slim/Slim.php';
require '../wp-config.php';
\Slim\Slim::registerAutoloader();
/**
* Step 2: Instantiate a Slim application
*
* This example instantiates a Slim application using
* its default settings. However, you will usually configure
* your Slim application now by passing an associative array
* of setting names and values into the application constructor.
*/
$app = new \Slim\Slim();
/**
* Step 3: Define the Slim application routes
*
* Here we define several Slim application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete`
* is an anonymous function.
*/
$app->get('/post/:id',function($id){
global $app;
$post = apc_fetch("post_{$id}");
if(!$post){
$query = new WP_Query("p={$id}");
if($query->post_count==0){
apc_add("post_{$id}",'404',12);
$post = '404';
}else{
$post = $query->posts[0];
apc_add("post_{$id}",$post,12);
}
}
if($post=='404'){
$app->pass();
}else{
header('HTTP/1.1 200 OK');
echo json_encode($post);
}
})->conditions(array('id'=>'\d+'));
$app->get('/posts/:mod/:id',function($mod,$id){
switch($mod){
case 'tag':
break;
case 'category':
if(!$cat = apc_fetch("cat_{$id}")){
$query = new WP_Query("cat={$id}");
apc_store("cat_{$id}",$query->posts,12);
$cat=$query->posts;
}
echo json_encode($cat);
}
})->conditions(array('mod'=>'\w+','id'=>'\d+'));
$app->get('/user/login_stat',function(){
var_dump($_GET);
echo json_encode(array('status'=>!!wp_get_current_user()->ID));
});
$app->post('/post',function(){
global $app;
if(!wp_get_current_user()->ID){
$app->halt(403,'You should login to publish a post.');
return;
}
if(!current_user_can('publish_posts')){
$app->halt(403,'You have no permission to publish a post.');
return;
}
if(!isset($_POST['title'])or!isset($_POST['content'])or!isset($_POST['tags'])or!isset($_POST['cats'])){
}
});
/**
* Step 4: Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment