0

I create a plugin and install it in any wordpress sites. For any reason I need to call a function on those plugin from another site (not wordpress site).

I've already try to using cURL and file_get_contents to call the function, in some wordpress site it's worked well, but in some another wordpress site it's not working.

It's failed on calling the function because the wordpress site has the redirect URL or the wordpress site has another plugins installed like captcha, security, etc.

These are the code/function in my plugins:

Class Myplugin{

 ..........

  function get_wp_version(){
    // to do
  }

  function call_api(){
    if($_GET['get_wp_version'] && $_GET['token']){
       $wp_version = $this->get_wp_version();
       echo $wp_version;
    }
  }
........
}

And these are the code that I used to call the function from another site:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://mysite.com/?call_api=get_wp_version&token=xxx');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
$curl_msg = curl_exec($ch);

Any help will be appreciated!

Oops, typo! The functin of my plugin should be like this:

Class Myplugin{

 ..........

  function get_wp_version(){
    // to do
  }

  function call_api(){
    if($_GET['call_api'] && $_GET['token']){
      if($_GET['call_api'] == 'get_wp_version'){
        $wp_version = $this->get_wp_version();
        echo $wp_version;
      }
    }
  }
........
}
4
  • What is the question? If security measures are blocking your access then you need to refine them or use something else like XML-RPC – Mark Kaplun Aug 7 '14 at 4:41
  • My question is how can my curl script can bypass the redirect-url or the captcha. So I can get the results from the function that I call. – arhakim Aug 7 '14 at 4:46
  • If you could bypass a security plugin there would not have been much reason to use it at all..... – Mark Kaplun Aug 7 '14 at 5:06
  • Yes that what I want but I still doesn't know how to bypass any security of those plugins. Do you have any suggestion on what should I do? – arhakim Aug 7 '14 at 5:42
2

There are two way you can implement this

1. Use the AJAX API

This is the fastest way of doing it. Just register an ajax action and use that action url to send requests.

Example

add_action('wp_ajax_api-call', 'wpse_156943_ajax_api_handle_request');

function wpse_156943_ajax_api_handle_request(){
    // security check validation

    // do whatever you want to do
}

The url for the request will be like http://example.com/wp-admin/admin-ajax.php?action=api-call

2. Create an URL Endpoint

Create a lot user friendly endpoint. For example http://example.com/my-api

Example

add_action( 'wp_loaded', 'wpse156943_internal_rewrites' );
function wpse156943_internal_rewrites(){
    add_rewrite_rule( 'my-api$', 'index.php?my-api=1', 'top' );
}

add_filter( 'query_vars', 'wpse156943_internal_query_vars' );
function wpse156943_internal_query_vars( $query_vars ){
    $query_vars[] = 'my-api';
    return $query_vars;
}

add_action( 'parse_request', 'wpse156943_internal_rewrites_parse_request' );
function wpse156943_internal_rewrites_parse_request( &$wp ){

    if (!array_key_exists( 'my-api', $wp->query_vars ) ) {
        return;
    }

    // security and validation 

    // do whatever you want to do

    die();
}

Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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