Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use API in wordpress

I would like to connect one Razorpay Payment API with WordPress, API has authentication token using username and password.

is there any builtin functionality available in WordPress to make a call and handle response?

like image 210
Aiyaz Khorajia Avatar asked Oct 25 '17 05:10

Aiyaz Khorajia


People also ask

What can I do with WordPress API?

The WordPress REST API allows developers to interact with WordPress sites remotely by sending and receiving JSON (JavaScript Object Notation) objects. This means now you can build websites, mobile apps, desktop apps, all based on WordPress from the back-end, but “without” WordPress on the front-end.

Is there an API for WordPress?

However, WordPress is a distributed API, meaning there isn't just one place to get all the data from. Each website running WordPress is a unique application, with unique users and authentication.


1 Answers

you can use wp_remote_get()

for example

wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );

you can also control all request parameters like headers and body data.

Default Usage

global $wp_version;
$args = array(
    'timeout'     => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
    'blocking'    => true,
    'headers'     => array(),
    'cookies'     => array(),
    'body'        => null,
    'compress'    => false,
    'decompress'  => true,
    'sslverify'   => true,
    'stream'      => false,
    'filename'    => null
); 

Reference : More info

like image 130
Thamaraiselvam Avatar answered Nov 01 '22 14:11

Thamaraiselvam