[wp-hackers] Unified HTTP Request API

Jacob wordpress at santosj.name
Fri Nov 16 06:13:58 GMT 2007


I brought this up in the past [1] and was brought up again[2] on trac. 
It seems that the moment is right to jump start it again. I wasn't too 
happen with the prototypes I started before. The goal has to be that it 
is as quick as possible and as easy as possible. It is only meant to be 
internal replacement for fsockopen function calls and not something that 
is fully functional. Meaning that it is not a replacement for Snoopy.

The base structure that I have so far is

class WP_HTTP_Base
{
    function test() { }
    function post($headers, $body) { }
    function get($headers, $body) { }
    function request($url, $headers=null, $body=null) { }
    function setHeaders($headers) { }
    function getHeaders($headers) { }
    function setBody($body) { }
    function getBody($body) { }
    function setTimeout($timeout) { }
}

class WP_HTTP_Fsockopen extends WP_HTTP_Base { }
class WP_HTTP_Fopen extends WP_HTTP_Base { }
class WP_HTTP_Curl extends WP_HTTP_Base { }
class WP_HTTP_Streams extends WP_HTTP_Base { }
class WP_HTTP_ExtHTTP extends WP_HTTP_Base { }

The purpose of the test() method is to see if the method can be used 
which would ask a few questions. It would be used to first test the 
server environment and then save the answer so that it doesn't have to 
keep asking. It will save to the options and check periodically. Based 
on the preferred list for either PHP 4 or PHP 5, the first one that 
succeeds would be used to fetch a URL. If all fail, then an user error 
would be thrown. It could also be used for the install process to check 
whether the server would allow for fetching URLs.

For Fsockopen:

1. Is Fsockopen in the disabled functions list.

For Fopen:

1. Is Allow_url_fopen set to enable (all PHP version supported by WordPress)
2. Is fopen in the disabled functions list.

For cURL:

1. Is the cURL extension installed and available.

For Streams:

It seems that prior to 4.3.0, all external URLs had to be retrieved 
using fsockopen, because streams mean using different protocols in 
fopen. This is actually, the $context usage allowed in PHP 5.0 and thus 
uses fopen and stream context usage. Supposely benchmarks (debatable) 
state that this method is faster.

1. Is PHP version greater than 5.0
2. Is Allow_url_fopen enabled

For ExtHTTP:

1. Is HTTP extension installed (and perhaps whether PHP 5.2 is 
installed, however the extension doesn't need PHP 5.2).

The rest of the methods should be self explanatory. Yeah, it doesn't do 
a lot, but it isn't meant to. It fetches the URL and allows for both the 
body and headers to be parsed separately.

The complete function will rest in a single function, 
wp_fetch_remote_url($url, $headers, $body), which will test all the 
classes and return the result from the ones that succeeds or return 
false if none of them do.

I'm writing for input on any thoughts on the current prototyped API and 
thoughts on whether such an patch would be worth placing in the core of 
WordPress. Are any WordPress devs working on something similar?

[1] http://trac.wordpress.org/ticket/4779
[2] http://trac.wordpress.org/ticket/5065

-- 

Jacob Santos

http://www.santosj.name - blog
http://wordpress.svn.dragonu.net/unittest/ - unofficial WP unit test suite.

Also known as darkdragon and santosj on WP trac.



More information about the wp-hackers mailing list