SlideShare a Scribd company logo
1 of 21
Integrating External APIs
Freelance WordPress Developer 8+ years
Owner and Lead Developer at
PhotographyBlogSites.com
martythornley.com @martythornley
A Little About Me
Application Program Interface
Allows programs
to communicate
with each other
What is an API?
Get information
Send information
What do APIs do?
TwitterDisplay Tweets
Yelp
Display Listings &
Search
Amazon
List & Sell
Products
Your Website or App
What do APIs do?
TwitterPost Tweets
Your Website or App
Facebook
Send new status,
images, location
InstagramCreate Likes
https://codex.wordpress.org/WordPress_APIs
Internal WordPress APIs
Makes plugins possible
Makes talking to the database easier
Makes theme customization possible
Makes communicating with external API’s easier
HTTP API
Database API
Options API
Transients API
Metadata API
Plugin API
https://codex.wordpress.org/WordPress_APIs
Internal WordPress APIs
Dashboard Widgets API
File Header API
Filesystem API
Quicktags API
Rewrite API
Shortcode API
Theme Modification API
Theme Customization API
Widgets API
XML-RPC WordPress API
Settings API
How To Use APIs
Endpoint
Authentication
Methods
Arguments
Response
URL where API ‘lives’
May be public or require authentication
Possible ways of using the API
Specify what you want it to do
What it sends back
Authenticate with APIs
None
Basic
OAuth
Not typical
Encoded username:password combo
or an API KEY
Complicated
WordPress HTTP API
wp_remote_get
wp_remote_post
Retrieve URL using GET HTTP method
Retrieve URL using POST HTTP method
https://codex.wordpress.org/HTTP_API
$response = wp_remote_get( $url , $args );
Response from an API
Array
(
[headers] => Array
(
[content-type] => text/html; charset=utf-8
[date] => Wed, 25 Mar 2015 03:43:43 GMT
[server] => Mashape/5.0.6
[via] => 1.1 vegur
[x-powered-by] => Express
[content-length] => 51
[connection] => Close
)
[body] => THE STUFF WE WANT
[response] => Array
(
[code] => 200
[message] => OK
)
[cookies] => Array
(
)
[filename] =>
)
Response from an API
Array
(
[headers] => Array
(
[content-type] => text/html; charset=utf-8
[date] => Wed, 25 Mar 2015 03:43:43 GMT
[server] => Mashape/5.0.6
[via] => 1.1 vegur
[x-powered-by] => Express
[content-length] => 51
[connection] => Close
)
[body] => THE STUFF WE WANT
[response] => Array
(
[code] => 200
[message] => OK
)
[cookies] => Array
(
)
[filename] =>
)
WordPress HTTP API
https://codex.wordpress.org/HTTP_API
$headers = wp_remote_retrieve_headers( $response );
$headers = wp_remote_retrieve_header( $response );
$code = wp_remote_retrieve_response_code( $response );
$message = wp_remote_retrieve_response_message( $response );
$body = wp_remote_retrieve_body( $response );
Error Checking Response
$response = wp_remote_get( $url , $args );
$code = wp_remote_retrieve_code( $response );
if ( $code == ‘200’ ) {
$body = wp_remote_retrieve_body( $response );
}
What Format is Response?
string
xml
json
$array = json_decode( $body );
$json = json_encode( $array );
Caching Response
Play nice with the API / Save your rate limits
Don’t wait on external services when possible
Use WordPress API’s to help
Options
Transients
WP_Object_Cache
Meta
Save to db
Save to db with time limit
Cache in memory
User / Post Meta
Caching Response
set_transient( ‘my_transient_name’ , $body , 30 );
get_transient( ‘my_transient_name’ );
delete_transient( ‘my_transient_name’ );
set_site_transient( ‘my_transient_name’ , $body , 30 );
get_site_transient( ‘my_transient_name’ );
delete_site_transient( ‘my_transient_name’ );
https://codex.wordpress.org/Transients_API
Caching Response
$body = get_transient( ‘my_transient_name’ );
if ( $body !== false ) {
$response = wp_remote_get( $url , $args );
$code = wp_remote_retrieve_code( $response );
if ( $code == ‘200’ ) {
$body = wp_remote_retrieve_body( $response );
set_transient( ‘my_transient_name’ , $body , 30 );
}
}
Example API Plugin
Yoda Speak
http://github.org/martythornley/yoda-speak
Where to find APIs
http://www.programmableweb.com/
https://www.mashape.com
http://apis.io/
Questions
martythornley@gmail.com
martythornley.com
photographyblogsites.com

More Related Content

What's hot

엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...
엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...
엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...
Amazon Web Services Korea
 

What's hot (20)

엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...
엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...
엔터프라이즈의 효과적인 클라우드 도입을 위한 전략 및 적용 사례-신규진 프로페셔널 서비스 리드, AWS/고병률 데이터베이스 아키텍트, 삼성...
 
Top Ten Web Hacking Techniques of 2012
Top Ten Web Hacking Techniques of 2012Top Ten Web Hacking Techniques of 2012
Top Ten Web Hacking Techniques of 2012
 
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
 
[2018] MySQL 이중화 진화기
[2018] MySQL 이중화 진화기[2018] MySQL 이중화 진화기
[2018] MySQL 이중화 진화기
 
ProxySQL & PXC(Query routing and Failover Test)
ProxySQL & PXC(Query routing and Failover Test)ProxySQL & PXC(Query routing and Failover Test)
ProxySQL & PXC(Query routing and Failover Test)
 
Airflow tutorials hands_on
Airflow tutorials hands_onAirflow tutorials hands_on
Airflow tutorials hands_on
 
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
 
flask.pptx
flask.pptxflask.pptx
flask.pptx
 
SQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they work
 
Spring Cloud Workshop
Spring Cloud WorkshopSpring Cloud Workshop
Spring Cloud Workshop
 
gRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at SquaregRPC: The Story of Microservices at Square
gRPC: The Story of Microservices at Square
 
Node.js File system & Streams
Node.js File system & StreamsNode.js File system & Streams
Node.js File system & Streams
 
How to configure hikvision dvr cloud storage through web browser
How to configure hikvision dvr cloud storage through web browserHow to configure hikvision dvr cloud storage through web browser
How to configure hikvision dvr cloud storage through web browser
 
Accessible Salesforce
Accessible SalesforceAccessible Salesforce
Accessible Salesforce
 
파이썬 데이터베이스 연결 1탄
파이썬 데이터베이스 연결 1탄파이썬 데이터베이스 연결 1탄
파이썬 데이터베이스 연결 1탄
 
[2019] 바르게, 빠르게! Reactive를 품은 Spring Kafka
[2019] 바르게, 빠르게! Reactive를 품은 Spring Kafka[2019] 바르게, 빠르게! Reactive를 품은 Spring Kafka
[2019] 바르게, 빠르게! Reactive를 품은 Spring Kafka
 
iOS Automation: XCUITest + Gherkin
iOS Automation: XCUITest + GherkiniOS Automation: XCUITest + Gherkin
iOS Automation: XCUITest + Gherkin
 
Spring Boot - Uma app do 0 a Web em 30 minutos
Spring Boot - Uma app do 0 a Web em 30 minutosSpring Boot - Uma app do 0 a Web em 30 minutos
Spring Boot - Uma app do 0 a Web em 30 minutos
 
Getting out of Callback Hell in PHP
Getting out of Callback Hell in PHPGetting out of Callback Hell in PHP
Getting out of Callback Hell in PHP
 
Java 8 - collections et stream
Java 8 - collections et streamJava 8 - collections et stream
Java 8 - collections et stream
 

Similar to Integrating External APIs with WordPress

P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
phelios
 
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
lanslote
 

Similar to Integrating External APIs with WordPress (20)

Restful design at work v2.0
Restful design at work v2.0Restful design at work v2.0
Restful design at work v2.0
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIs
 
Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.Best Practices for Architecting a Pragmatic Web API.
Best Practices for Architecting a Pragmatic Web API.
 
SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...
SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...
SharePoint Saturday Ottawa 2015 - Office 365 and PowerShell - A match made in...
 
mashraqi_farhan
mashraqi_farhanmashraqi_farhan
mashraqi_farhan
 
Webinar - Office 365 & PowerShell : A Match Made in Heaven
Webinar - Office 365 & PowerShell : A Match Made in HeavenWebinar - Office 365 & PowerShell : A Match Made in Heaven
Webinar - Office 365 & PowerShell : A Match Made in Heaven
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
PHP on Windows and on Azure
PHP on Windows and on AzurePHP on Windows and on Azure
PHP on Windows and on Azure
 
API Design Tour: Dell
API Design Tour: DellAPI Design Tour: Dell
API Design Tour: Dell
 
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
[SharePoint Korea Conference 2013 / 강율구] Sharepoint 스마트하게 개발하기
 
WORDPRESS_REST_API_WORDPRESS_REST_API.pdf
WORDPRESS_REST_API_WORDPRESS_REST_API.pdfWORDPRESS_REST_API_WORDPRESS_REST_API.pdf
WORDPRESS_REST_API_WORDPRESS_REST_API.pdf
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW Workshop
 
An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
 
forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Api testing
Api testingApi testing
Api testing
 
Word press rest api sudarshan shrestha
Word press rest api  sudarshan shresthaWord press rest api  sudarshan shrestha
Word press rest api sudarshan shrestha
 
Exposing Salesforce REST Services Using Swagger
Exposing Salesforce REST Services Using SwaggerExposing Salesforce REST Services Using Swagger
Exposing Salesforce REST Services Using Swagger
 
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
WordCamp Raleigh 2016 - WP API, What is it good for? Absolutely Everything!
 
Using WordPress as your application stack
Using WordPress as your application stackUsing WordPress as your application stack
Using WordPress as your application stack
 

Recently uploaded

VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Pollachi 7001035870 Whatsapp Number, 24/07 Booking
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
Dubai Call Girls Milky O525547819 Call Girls Dubai Soft Dating
Dubai Call Girls Milky O525547819 Call Girls Dubai Soft DatingDubai Call Girls Milky O525547819 Call Girls Dubai Soft Dating
Dubai Call Girls Milky O525547819 Call Girls Dubai Soft Dating
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
Enjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Samalka Delhi >༒8448380779 Escort Service
 
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Prashant Vihar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 

Integrating External APIs with WordPress

  • 2. Freelance WordPress Developer 8+ years Owner and Lead Developer at PhotographyBlogSites.com martythornley.com @martythornley A Little About Me
  • 3. Application Program Interface Allows programs to communicate with each other What is an API? Get information Send information
  • 4. What do APIs do? TwitterDisplay Tweets Yelp Display Listings & Search Amazon List & Sell Products Your Website or App
  • 5. What do APIs do? TwitterPost Tweets Your Website or App Facebook Send new status, images, location InstagramCreate Likes
  • 6. https://codex.wordpress.org/WordPress_APIs Internal WordPress APIs Makes plugins possible Makes talking to the database easier Makes theme customization possible Makes communicating with external API’s easier
  • 7. HTTP API Database API Options API Transients API Metadata API Plugin API https://codex.wordpress.org/WordPress_APIs Internal WordPress APIs Dashboard Widgets API File Header API Filesystem API Quicktags API Rewrite API Shortcode API Theme Modification API Theme Customization API Widgets API XML-RPC WordPress API Settings API
  • 8. How To Use APIs Endpoint Authentication Methods Arguments Response URL where API ‘lives’ May be public or require authentication Possible ways of using the API Specify what you want it to do What it sends back
  • 9. Authenticate with APIs None Basic OAuth Not typical Encoded username:password combo or an API KEY Complicated
  • 10. WordPress HTTP API wp_remote_get wp_remote_post Retrieve URL using GET HTTP method Retrieve URL using POST HTTP method https://codex.wordpress.org/HTTP_API $response = wp_remote_get( $url , $args );
  • 11. Response from an API Array ( [headers] => Array ( [content-type] => text/html; charset=utf-8 [date] => Wed, 25 Mar 2015 03:43:43 GMT [server] => Mashape/5.0.6 [via] => 1.1 vegur [x-powered-by] => Express [content-length] => 51 [connection] => Close ) [body] => THE STUFF WE WANT [response] => Array ( [code] => 200 [message] => OK ) [cookies] => Array ( ) [filename] => )
  • 12. Response from an API Array ( [headers] => Array ( [content-type] => text/html; charset=utf-8 [date] => Wed, 25 Mar 2015 03:43:43 GMT [server] => Mashape/5.0.6 [via] => 1.1 vegur [x-powered-by] => Express [content-length] => 51 [connection] => Close ) [body] => THE STUFF WE WANT [response] => Array ( [code] => 200 [message] => OK ) [cookies] => Array ( ) [filename] => )
  • 13. WordPress HTTP API https://codex.wordpress.org/HTTP_API $headers = wp_remote_retrieve_headers( $response ); $headers = wp_remote_retrieve_header( $response ); $code = wp_remote_retrieve_response_code( $response ); $message = wp_remote_retrieve_response_message( $response ); $body = wp_remote_retrieve_body( $response );
  • 14. Error Checking Response $response = wp_remote_get( $url , $args ); $code = wp_remote_retrieve_code( $response ); if ( $code == ‘200’ ) { $body = wp_remote_retrieve_body( $response ); }
  • 15. What Format is Response? string xml json $array = json_decode( $body ); $json = json_encode( $array );
  • 16. Caching Response Play nice with the API / Save your rate limits Don’t wait on external services when possible Use WordPress API’s to help Options Transients WP_Object_Cache Meta Save to db Save to db with time limit Cache in memory User / Post Meta
  • 17. Caching Response set_transient( ‘my_transient_name’ , $body , 30 ); get_transient( ‘my_transient_name’ ); delete_transient( ‘my_transient_name’ ); set_site_transient( ‘my_transient_name’ , $body , 30 ); get_site_transient( ‘my_transient_name’ ); delete_site_transient( ‘my_transient_name’ ); https://codex.wordpress.org/Transients_API
  • 18. Caching Response $body = get_transient( ‘my_transient_name’ ); if ( $body !== false ) { $response = wp_remote_get( $url , $args ); $code = wp_remote_retrieve_code( $response ); if ( $code == ‘200’ ) { $body = wp_remote_retrieve_body( $response ); set_transient( ‘my_transient_name’ , $body , 30 ); } }
  • 19. Example API Plugin Yoda Speak http://github.org/martythornley/yoda-speak
  • 20. Where to find APIs http://www.programmableweb.com/ https://www.mashape.com http://apis.io/