Consuming & embedding
external content
Akshay Raje
@akshayraje
profiles.wordpress.org/akshay_raje
WordPress
jQuery
Human Resources
Biking
PHPJavaScript
$topics = array(
'HTTP requests in PHP/WordPress',
'Helper functions: GET, POST, HEAD',
'Request arguments',
'Caching: Object Caching and Transients API',
'Parsing',
'How WP Web Scraper helps'
);
if( in_array( $question, $topics ) !== true )
var_dump( $question );
Next 25 minutes:
@akshayraje
0: HTTP Requests in
PHP/WordPress
@akshayraje
Multiple Transports, Functions,
Environments
cURL
file_get_contents
fsockopen
Shared
Dedicated
VPS
IaaS
PaaS
fopenhttp
streams
Before AfterWord
2
Press
7
• Design & support own
implementation
• Branch code for multiple
transport implementations
• Duplication of code across
plugins
• Standard API for core and
plugins
• Wider environment support
with fallbacks
• OO and helper Functional
@akshayraje
1: Helper functions: GET, POST,
HEAD
@akshayraje
wp_remote_get() Retrieves a URL using the GET
HTTP method.
wp_remote_post() Retrieves a URL using the POST
HTTP method.
wp_remote_head() Retrieves a URL using the HEAD
HTTP method.
wp_remote_request() Retrieves a URL using either the
default GET or a custom HTTP
method that you specify.
WP_HTTP Helper functions
@akshayraje
$url (string) (required) Site URL to retrieve.
Default: None
$args (array) (optional) Override the defaults.
Default: array()
WP_HTTP Helper functions
Parameters
Return Values
(WP_Error|array) The response or WP_Error on failure.
@akshayraje
$response = wp_remote_get( 'http://httpbin.org/html' );
if ( is_wp_error( $response ) ) {
echo 'Something went wrong: ' . $response-
>get_error_message();
} else {
print_r( $response );
}
<?code
@akshayraje
@akshayraje
<?output
@akshayraje
2: Request arguments
@akshayraje
@akshayraje
Default $args
@akshayraje
$args = array(
'timeout' => 10,
'user-agent' => 'My-Bot',
'body' => array( 'username' => 'bob', 'password' => '1234xyz' )
);
$response = wp_remote_post( 'http://httpbin.org/post', $args );
if ( is_wp_error( $response ) ) {
echo 'Something went wrong: ' . $response->get_error_message();
} else {
print_r( $response );
}
<?code POST example
3: Caching: Object Cache and
Transients API
@akshayraje
Why Cache?
API Rate Limits
Blocking requests
Outgoing bandwidth
Avoid abusing web service
Page Load
No change in content
PaaS Rate Limits
What Cache?
Object Cache
Page Cache
@akshayraje
What Cache?
Object Cache
Persistent
Page Cache
@akshayraje
Object Cache
Persistent
W3 Total Cache
WP Super Cache
Quick Cache
Hyper Cache
WP Fastest Cache
Disk
APC
eAccelerator
XCache
WinCache
Memcached
@akshayraje
Object Cache
Persistent
Transients API
‘guarantees’
@akshayraje
Use the Transients API instead of
wp_cache functions if you need to
guarantee that your data will be cached. If
persistent caching is configured, then the
transients functions will use the wp_cache
functions. However if persistent caching
has not been enabled, then the data will
instead be cached to the options table.
Source: http://codex.wordpress.org/Class_Reference/WP_Object_Cache
“
@akshayraje
@akshayraje
$transient (string) (required) Unique transient name.
45 characters or less in length.
Default: None
Transients functions
get_transient( $transient )
Pretty much like get_option( $option )
@akshayraje
Transients functions
set_transient($transient, $value, $expiration)
Pretty much like set_option( $option, $value )
$transient (string) (required) Unique transient name.
45 characters or less in length.
Default: None
$value (mixed) (required) Transient value.
Default: None
$expiration (int) (optional) Time until expiration in
seconds from now, or 0 for never expires.
Default: 0.
@akshayraje
// Get any existing copy of transient data if available
if ( false === ( $response = get_transient( 'special_wp_remote_get' ) ) ) {
$response = wp_remote_get( 'http://httpbin.org/html' );
if ( !is_wp_error( $response ) ) {
set_transient( 'special_wp_remote_get', $response, 12 *
HOUR_IN_SECONDS );
}
}
// Use $response like you would have normally...
<?code Cached wp_remote_get
4: Parsing
@akshayraje
<?output
@akshayraje
wp_remote_retrieve_body() Retrieves just the body from the
response.
wp_remote_retrieve_header() Gives you a single HTTP header
based on name from the
response.
wp_remote_retrieve_headers() Returns all of the HTTP headers
in an array for processing.
wp_remote_retrieve_response_code() Gives you the number for the
HTTP response. This should be
200, but could be 4xx or even
3xx on failure.
wp_remote_retrieve_response_message() Returns the response message
based on the response code.
WP_HTTP Helper functions
@akshayraje
JSON Native Object
json_decode
XML XPath
DOMDocument class
HTML XPath
DOMDocument class
Parsing
@akshayraje
5: WP Web Scraper
wp-ws.net
wordpress.org/plugins/wp-web-scrapper
@akshayraje

Consuming & embedding external content in WordPress