consuming & embedding external content in wordpress
Post on 14-Aug-2015
401 Views
Preview:
TRANSCRIPT
Consuming & embedding external content
Akshay Raje
@akshayrajeprofiles.wordpress.org/akshay_raje
WordPressjQueryHuman 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
fsockopenShared
Dedicated
VPS
IaaS
PaaS
fopenhttp
streams
Before AfterWord2
Press7
• 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 requestsOutgoing bandwidth
Avoid abusing web service
Page Load
No change in contentPaaS Rate Limits
What Cache?
Object Cache
Page Cache
@akshayraje
What Cache?
Object CachePersistent
Page Cache
@akshayraje
Object CachePersistent
W3 Total CacheWP Super CacheQuick CacheHyper CacheWP Fastest Cache
DiskAPC
eAcceleratorXCache
WinCacheMemcached
@akshayraje
Object CachePersistent
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 Objectjson_decode
XML XPath DOMDocument class
HTML XPathDOMDocument class
Parsing
@akshayraje
5: WP Web Scraper
wp-ws.networdpress.org/plugins/wp-web-scrapper
@akshayraje
top related