Last active
June 12, 2024 19:11
-
-
Save igorbenic/b2c7a1640603c732414d852e0f027f72 to your computer and use it in GitHub Desktop.
Handle WordPress Remote Requests with OOP | ibenic.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$remote_request = new WordPressRemoteJSON( 'https://leanpub.com/wpb3/coupons.json', array( 'body' => array("coupon_code" => "coupon-code-123456" ) ), "post" ); | |
$remote_request->run(); //False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$remote_request = new WordPressRemoteJSON( 'https://leanpub.com/wpb3.json' ); | |
$remote_request->run(); //True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressRemote { | |
/** | |
* Request method | |
* @var string | |
*/ | |
protected $method = ""; | |
/** | |
* URL where to send the request | |
* @var string | |
*/ | |
protected $url = ''; | |
/** | |
* Arguments applied to the request | |
* @var array | |
*/ | |
protected $arguments = array(); | |
/** | |
* Request response | |
* @var [type] | |
*/ | |
protected $response; | |
/** | |
* Body of the response | |
* @var [type] | |
*/ | |
protected $body; | |
/** | |
* Headers of the response | |
* @var [type] | |
*/ | |
protected $headers; | |
/** | |
* Response Code from the Request | |
* @var [type] | |
*/ | |
protected $response_code; | |
/** | |
* Response Message from the Request | |
* @var [type] | |
*/ | |
protected $response_message; | |
/** | |
* Creating the object | |
* @param string $url | |
* @param array $array | |
* @param string $method | |
*/ | |
public function __construct( $url, $array = array(), $method = "get" ) { | |
$this->method = strtoupper( $method ); | |
$this->url = $url; | |
$this->arguments = $array; | |
$this->arguments['method'] = $this->method; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressRemote { | |
// ... | |
/** | |
* Running the request and setting all the attributes | |
* @return void | |
*/ | |
public function run() { | |
$this->response = wp_remote_request( $this->url, $this->arguments ); | |
$this->body = wp_remote_retrieve_body( $this->response ); | |
$this->headers = wp_remote_retrieve_headers( $this->response ); | |
$this->response_code = wp_remote_retrieve_response_code( $this->response ); | |
$this->response_message = wp_remote_retrieve_response_message( $this->response ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressRemote { | |
// ... | |
/** | |
* Get the body | |
* @return mixed | |
*/ | |
public function get_body() { | |
return $this->body; | |
} | |
/** | |
* Get the headers | |
* @return mixed | |
*/ | |
public function get_headers() { | |
return $this->headers; | |
} | |
/** | |
* Response Code | |
* @return string | |
*/ | |
public function get_response_code() { | |
return $this->response_code; | |
} | |
/** | |
* Get the response message | |
* @return string | |
*/ | |
public function get_response_message() { | |
return $this->response_message; | |
} | |
/** | |
* Get the whole response | |
* @return mixed | |
*/ | |
public function get_response() { | |
return $this->response; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressRemote { | |
// ... | |
/** | |
* If the request was a success | |
* @return mixed | |
*/ | |
public function is_success() { | |
if( $this->response_code == '200' ) { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WordPressRemoteJSON extends WordPressRemote { | |
/** | |
* Prepare the headers for JSON requests and then run the main method run() | |
**/ | |
public function run() { | |
$this->arguments['headers']['Content-type'] = 'application/json'; | |
parent::run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment