API Wrapper Function for WordPress


This is a simple wrapper function for all API calls using the WordPress wp_remote() function.

/***************************
* WRAPPER FUNCTION FOR ALL PAGELY API REQUESTS	
* This function makes a remote request to the Pagely REST API
* @param string $method GET | POST | PUT | DELETE 
* @param string $uri the controller and method to call on teh api
* @param array $params required data for method
* @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
* @return[body] returned will be a json response, see our API docs
*/
function _pagely_api_request($method = 'GET', $uri = '/controller/method', $params = array() ) {
	// setup the request
	$url 		= $this->api_endpoint . $uri;
	$headers = array( 'X-API-KEY' => $this->_api_key());

	// switch based on METHOD		
	/***********
	// GET is for getting reecords
	// POST is for updating existing records and requires an ID
	// PUT is for create NEW records
	// DELETE is for remove records and requires an ID
	************/

	switch ($method) {
		case 'GET':
			$params['sess']		= ''; 
			$querystring		= http_build_query($params);
			// append a query string to the url
			$url 			= $url.'?'.$querystring;
			// unset params on GET
			$params 		= false;
		break;
		case 'POST':
		case 'PUT':
		case 'DELETE':
		// generate some secure hashes			
			$time    		= date('U');
			$params['sess']		= ''; 
			$params['time']    	= $time;
   			$params['hash']    	= sha1($time.$this->pagely_api_sec);
   			// pass an object ID as needed
   			$params['id']		= $params['id']; // should be object id, like domain_id = 1099; can be empty on PUT
		break;
	}

	// make the request
	$req_args = array(
		'method' => $method, 
		'body' => $params, 
		'headers' => $headers, 
		'sslverify' => true  // set to true in live envrio
	);

	// make the remote request
	$result = wp_remote_request( $url, $req_args);

        // handle response
	if ( !is_wp_error($result) ) {
		//no error
		$this->_log_api_reponse($url);
		$this->_log_api_reponse($params);
		$this->_log_api_reponse($result['body']);
		return $result['body'];	
	} else {
	        // error
	        return $result->get_error_message();			
	}	
}

To call this function:

/*
* Test API
* @return array|object Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
* @return[body] returned will be a json response, see our API docs
*/  	

function _test_api() {

        $params = array();

	$uri = '/go/hello';
	$json = $this->_pagely_api_request($method = 'GET',$uri, $params );

	if (!json_decode($json)) {
		// was not a json response...make one
		$res->message = $json;
		$json = json_encode($res);
		return false;
	}

	$result = json_decode($json);					
	if (isset($result->result) && $result->result == 2) {
		// api works
		return true;
	} else {
		return false;
	}

}

Pagely® is the original Managed WordPress Hosting company. Since 2009 we have helped thousands of personal bloggers, small business, and enterprise clients secure and accelerate their WordPress powered sites. Let us help you.

The #1 rated WordPress Host - WPMU.org

Give Us a Try Today