[wp-trac] Re: [WordPress Trac] #4779: Proposal for HTTP POST and REQUEST API

WordPress Trac wp-trac at lists.automattic.com
Wed May 21 00:15:40 GMT 2008


#4779: Proposal for HTTP POST and REQUEST API
--------------------------------------------------+-------------------------
 Reporter:  darkdragon                            |        Owner:  anonymous
     Type:  enhancement                           |       Status:  new      
 Priority:  normal                                |    Milestone:  2.6      
Component:  Optimization                          |      Version:           
 Severity:  normal                                |   Resolution:           
 Keywords:  has-patch needs-testing dev-feedback  |  
--------------------------------------------------+-------------------------
Changes (by DD32):

  * keywords:  has-patch needs-testing => has-patch needs-testing dev-
               feedback

Comment:

 Is there any chance for 2.6 !WordPress can standardise on external access?

 Currently WordPress has:
  * Snoopy
  * fsockopen() in various function
  * Curl
  * fopen()
  * A Wrapper around Snoopy
  * A Wrapper around Curl && fopen() (I swear i saw that)

 Every piece of code tends to use a different one, Not only does that mean
 that certain code may work while another doesnt, It also doesnt help with
 those who want to set a proxy server, If it was wrapped up into a single
 function/method, It would greatly help those users.

 There was never much traction on this ticket from Devs, But it'd be good
 to get a final word as to if anyone is interested.

 I'm not too sure if the patches on here are the best though, But that
 depends on the direction devs want to take it. I'd be just as happy if
 fsockopen() was bound together into a single function which everything
 used(I use this in a few plugins):
 {{{
 function remote_http($url, $method='GET', $data='', $headers = array()){
         //Note: This returns all headers in lowercase.
         global $wp_version;
         //Partly stolen from update checker code :)
         if( is_array($data) )
                 $data = http_build_query($data);

         $allowed_methods = array('GET', 'POST', 'HEAD');
         if( ! in_array($method, $allowed_methods) ) $method = 'GET';

         $port = 80;
         $schema = 'http';
         $path = $host = $query = '';
         $parts = parse_url($url);
         extract($parts);

         $http_request  = "$method $path?$query HTTP/1.0\r\n";
         $http_request .= "Host: $host\r\n";
         $http_request .= 'Content-Type: application/x-www-form-urlencoded;
 charset=' . get_option('blog_charset') . "\r\n";
         if( $data )
                 $http_request .= 'Content-Length: ' . strlen($request) .
 "\r\n";
         $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' .
 '; ' . get_bloginfo('url') . "\r\n";
         if( ! empty($headers) )
                 foreach($headers as $header => $value)
                         $http_request .= "$header: $value\r\n";
         $http_request .= "\r\n";
         if( $data )
                 $http_request .= $request;

         $response = '';
         if( false == ( $fs = @fsockopen( $host, $port, $errno, $errstr, 3)
 ) || ! is_resource($fs) )
                 return false;

         fwrite($fs, $http_request);

         while ( !feof($fs) )
                 $response .= fgets($fs, 1160); // One TCP-IP packet
         fclose($fs);
         $response = explode("\r\n\r\n", $response, 2);

         $ret = (object)array('headers' => array(), 'content'=>
 $response[1]);

         foreach( explode("\n", $response[0]) as $rheader){
                 list($header, $value) = explode(':', $rheader, 2);
                 if( !empty($header) && ! empty($value) ) {
                         $ret->headers[ strtolower(trim($header)) ] =
 trim($value);
                 } else if( strpos($rheader, 'HTTP') > -1) {
                         list(,$status, $status_name) = preg_split('|\s+|',
 $rheader);
                         $ret->headers[ 'status' ] = $status;
                         $ret->headers[ 'status-name' ] = $status_name;
                 }
         }

         return $ret;
 }
 }}}
 At least it could then be improved on in the future if need be, But why
 not do it right, and get it in there now?

-- 
Ticket URL: <http://trac.wordpress.org/ticket/4779#comment:9>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list