{"id":1301,"date":"2009-08-16T20:30:29","date_gmt":"2009-08-16T18:30:29","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1301"},"modified":"2010-03-30T16:12:57","modified_gmt":"2010-03-30T14:12:57","slug":"how-to-make-http-requests-with-wordpress","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2009\/08\/how-to-make-http-requests-with-wordpress\/","title":{"rendered":"How To Make HTTP Requests with WordPress"},"content":{"rendered":"<p>Making HTTP requests in PHP is not difficult, and a variety of methods exists: using <tt>fopen()<\/tt>, using cURL extension, using file streams with <tt>fsockopen()<\/tt> and <tt>fwrite()<\/tt> for instance. The problem is: depending on server setup, this might or might not work on another server. The good new is: once again, WordPress has a no-brainer API that makes it dead easy and compatible with all environments.<br \/>\n<!--more--><\/p>\n<h2>Introducing PHP class WP_Http<\/h2>\n<p>WordPress, since version 2.7 and thanks to the extreme coding leetness of <a href=\"http:\/\/jacobsantos.com\/\">Jacob Santos<\/a>, has a nifty class that sits in your <tt>wp-includes<\/tt> directory, in file <tt>http.php<\/tt>: <a href=\"http:\/\/core.trac.wordpress.org\/browser\/trunk\/wp-includes\/http.php\">WP_Http<\/a><\/p>\n<p><strong>WordPress 3.x update<\/strong>: you now need to explicitly include the class if you intend to use it, that is start by adding the following lines:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #990000\">class_exists<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'WP_Http'<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">include_once<\/span><span style=\"color: #009900\">&#040;<\/span> ABSPATH <span style=\"color: #339933\">.<\/span> WPINC<span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'\/class-http.php'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>The power and beauty of this class is that, depending on what&#39;s available on the server, it will pick up the best method to perform HTTP request. Don&#39;t bother checking for the presence of HTTP extension, fopen() behavior, existence of curl_init(), as this class will do all this for you.<\/p>\n<p>Basically, all the code you need to type is, more or less:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$request<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> WP_Http<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$result<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$request<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">request<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'http:\/\/some.url.you-need.to-fetch'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Variable <tt>$result<\/tt> will be an array, containing the following items:<\/p>\n<ul>\n<li><tt>'headers'<\/tt>: an array of response headers, such as <tt>\"x-powered-by\" => \"PHP\/5.2.1\"<\/tt><\/li>\n<li><tt>'body'<\/tt>: the response string sent by the server, as you would see it with you web browser<\/li>\n<li><tt>'response'<\/tt>: an array of HTTP response codes. Typically, you&#39;ll want to have <tt>array('code'=>200, 'message'=>'OK')<\/tt>\n<li><tt>'cookies'<\/tt>: an array of cookie information<\/li>\n<\/ul>\n<p>Now, let&#39;s see a few more complex examples and real world situations<\/p>\n<h2>Basic stuff: a simple GET request<\/h2>\n<p>Let&#39;s say your next plugin needs to poll Twitter&#39;s search service for latest tweets about rabbits. It&#39;s no more code than:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$url<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'http:\/\/search.twitter.com\/search.json?q=rabbits'<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$request<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> WP_Http<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$result<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$request<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">request<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$url<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$json<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$result<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'body'<\/span><span style=\"color: #009900\">&#093;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>This is all you need for a JSON encoded string containing what you&#39;re looking for.<\/p>\n<h2>Slightly more advanced: a standard POST request<\/h2>\n<p>If you need to pass some parameters, say nick= &#39;ozh&#39; and mood=&#39;happy&#39;, using the POST method, here is how you modify the request:<\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$body<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #0000ff\">'nick'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'ozh'<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;<span style=\"color: #0000ff\">'mood'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'happy'<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$url<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'http:\/\/your.api.url\/'<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$request<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> WP_Http<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$result<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$request<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">request<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$url<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'method'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'POST'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'body'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #000088\">$body<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ test $result['response'] and if OK do something with $result['body']<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h2>Cool stuff: playing with an API which requires authentication<\/h2>\n<p>Let&#39;s say you want to update your Twitter stream. Their <a href=\"http:\/\/apiwiki.twitter.com\/Twitter-REST-API-Method%3A-statuses%C2%A0update\">status update method<\/a> is a simple request, but it needs authentication (ie <a href=\"http:\/\/twitter.com\/statuses\/update.xml\">http:\/\/twitter.com\/statuses\/update.xml<\/a> asks for your Twitter login and password)<\/p>\n<p>Authentication in HTTP requests is achieved by sending a base64 encoded string of the username, a colon and the password (ie &quot;login:password&quot;)<\/p>\n<p>Our Twitter example would be something simple like:<\/p>\n<div id=\"ig-sh-5\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ You would edit the following:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$username<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'joe'<\/span><span style=\"color: #339933\">;<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ Twitter login<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$password<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'123456'<\/span><span style=\"color: #339933\">;<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ Twitter password<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$message<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">&quot;Hey neat, I'm posting with the API&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Now, the HTTP request:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$api_url<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'http:\/\/twitter.com\/statuses\/update.xml'<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$body<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'status'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #000088\">$message<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$headers<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'Authorization'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'Basic '<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #990000\">base64_encode<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">&quot;<span style=\"color: #006699;font-weight: bold\">$username<\/span>:<span style=\"color: #006699;font-weight: bold\">$password<\/span>&quot;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$request<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000000;font-weight: bold\">new<\/span> WP_Http<span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$result<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$request<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">request<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$api_url<\/span> <span style=\"color: #339933\">,<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'method'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'POST'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'body'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #000088\">$body<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'headers'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #000088\">$headers<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Can&#39;t be more simple, right?<\/p>\n<h2>Learn more<\/h2>\n<p>Jacob Santos being the documentation advocate he is, his class is fully commented, so just read the source to learn more. For instance, there are shortcuts to do GET, POST or HEAD request, you can pass more parameters such as a timeout limit, you can play with cookies and much more.<\/p>\n<h2>Please ditch the deprecated stuff<\/h2>\n<p>I almost feel stupid for writing an how-to guide for something that easy to use, but the thing is I have the feeling this class is heavily underused. For instance, as of writing I&#39;m reviewing the plugins that were submitted in the <a href=\"http:\/\/weblogtoolscollection.com\/pluginblog\/\">Plugin Competition 2009<\/a>, and this class is not exactly popular. Well, it should.<\/p>\n<p>Don&#39;t reinvent the wheel using cURL, it&#39;s not always installed. Don&#39;t use class Snoopy, it&#39;s completely deprecated. Just use WP_Http!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making HTTP requests in PHP is not difficult, and a variety of methods exists: using fopen(), using cURL extension, using file streams with fsockopen() and fwrite() for instance. The problem is: depending on server setup, this might or might not work on another server. The good new is: once again, WordPress has a no-brainer API [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[245,277,360],"class_list":["post-1301","post","type-post","status-publish","format-standard","hentry","category-published","tag-wordpress","tag-wordpress-snippet","tag-wp_http"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=1301"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1301\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}