0

I have create a simple API virtual page:

add_action( 'init', 'wpse9870_init_external' );    
function wpse9870_init_external()    
{    
    global $wp_rewrite;    
    $plugin_url = plugins_url( 'api.php', __FILE__ );    
    $plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );          
    $wp_rewrite->add_external_rule( 'api.php$', $plugin_url );    
}    
add_action( 'init', 'wpse9870_init_internal' );
function wpse9870_init_internal()
{
    add_rewrite_rule( 'api.php$', 'index.php?wpse9870_api=1', 'top' );
}
add_filter( 'query_vars', 'wpse9870_query_vars' );
function wpse9870_query_vars( $query_vars )
{
    $query_vars[] = 'wpse9870_api';
    return $query_vars;
}
add_action( 'parse_request', 'wpse9870_parse_request' );
function wpse9870_parse_request( &$wp )
{
    if ( array_key_exists( 'wpse9870_api', $wp->query_vars ) ) {
        include 'api.php';
        exit();
    }
    return;
}

Now the code:

if(strpos($_SERVER['REQUEST_URI'], "index.php?wpse9870_api=1"))    
{
   header("HTTP/1.1 200 OK");
   echo "v1p"; 
}

The page work and return "v1p" in simple test but header is 500 and if make a post with :

$.ajax({
            type: 'POST',
            url: '/index.php?wpse9870_api=1',

it return error on:

error: function(response) { alert(JSON.stringify(response)); });

{"readyState":4,"responseText":"v1p","status":500,"statusText":"Internal Server Error"}

Can you help me? Before changing server all work, now i have LAMP with Apache 2.4.27 and before cPanel.

1 Answer 1

0

I have solver with

die;

After echo.

if(strpos($_SERVER['REQUEST_URI'], "index.php?wpse9870_api=1"))    
{
   header("HTTP/1.1 200 OK");
   echo "v1p"; 
   die;
}

Header is now 200;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.