Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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: Obj...
0: HTTP Requests in
PHP/WordPress
@akshayraje
Multiple Transports, Functions,
Environments
cURL
file_get_contents
fsockopen
Shared
Dedicated
VPS
IaaS
PaaS
fopenhttp
str...
Before AfterWord
2
Press
7
• Design & support own
implementation
• Branch code for multiple
transport implementations
• Du...
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...
$url (string) (required) Site URL to retrieve.
Default: None
$args (array) (optional) Override the defaults.
Default: arra...
$response = wp_remote_get( 'http://httpbin.org/html' );
if ( is_wp_error( $response ) ) {
echo 'Something went wrong: ' . ...
@akshayraje
<?output
@akshayraje
2: Request arguments
@akshayraje
@akshayraje
Default $args
@akshayraje
$args = array(
'timeout' => 10,
'user-agent' => 'My-Bot',
'body' => array( 'username' => 'bob', 'password' => ...
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
P...
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
XCach...
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...
@akshayraje
$transient (string) (required) Unique transient name.
45 characters or less in length.
Default: None
Transient...
@akshayraje
Transients functions
set_transient($transient, $value, $expiration)
Pretty much like set_option( $option, $val...
@akshayraje
// Get any existing copy of transient data if available
if ( false === ( $response = get_transient( 'special_w...
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 h...
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
Upcoming SlideShare
Loading in …5
×

Consuming & embedding external content in WordPress

2,190 views

Published on

Consuming & embedding external content in WordPress presented by me at WordCamp Mumbai 2015 #WCMumbai

Published in: Software
  • Be the first to comment

Consuming & embedding external content in WordPress

  1. 1. Consuming & embedding external content Akshay Raje
  2. 2. @akshayraje profiles.wordpress.org/akshay_raje WordPress jQuery Human Resources Biking PHPJavaScript
  3. 3. $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
  4. 4. 0: HTTP Requests in PHP/WordPress @akshayraje
  5. 5. Multiple Transports, Functions, Environments cURL file_get_contents fsockopen Shared Dedicated VPS IaaS PaaS fopenhttp streams
  6. 6. 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
  7. 7. 1: Helper functions: GET, POST, HEAD @akshayraje
  8. 8. 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
  9. 9. $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
  10. 10. $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
  11. 11. @akshayraje <?output @akshayraje
  12. 12. 2: Request arguments @akshayraje
  13. 13. @akshayraje Default $args
  14. 14. @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
  15. 15. 3: Caching: Object Cache and Transients API @akshayraje
  16. 16. Why Cache? API Rate Limits Blocking requests Outgoing bandwidth Avoid abusing web service Page Load No change in content PaaS Rate Limits
  17. 17. What Cache? Object Cache Page Cache @akshayraje
  18. 18. What Cache? Object Cache Persistent Page Cache @akshayraje
  19. 19. Object Cache Persistent W3 Total Cache WP Super Cache Quick Cache Hyper Cache WP Fastest Cache Disk APC eAccelerator XCache WinCache Memcached @akshayraje
  20. 20. Object Cache Persistent Transients API ‘guarantees’ @akshayraje
  21. 21. 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
  22. 22. @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 )
  23. 23. @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.
  24. 24. @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
  25. 25. 4: Parsing @akshayraje
  26. 26. <?output @akshayraje
  27. 27. 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
  28. 28. JSON Native Object json_decode XML XPath DOMDocument class HTML XPath DOMDocument class Parsing @akshayraje
  29. 29. 5: WP Web Scraper wp-ws.net wordpress.org/plugins/wp-web-scrapper @akshayraje

×