Ticket #4779: 4779.remote_http.diff

File 4779.remote_http.diff, 3.5 KB (added by DD32, 5 years ago)
Line 
1Index: wp-includes/pluggable.php
2===================================================================
3--- wp-includes/pluggable.php   (revision 7986)
4+++ wp-includes/pluggable.php   (working copy)
5@@ -1445,4 +1445,99 @@
6 }
7 endif;
8 
9+if( ! function_exists('remote_http') ){
10+/**
11+ * remote_http() - makes a HTTP/1.0 request.
12+ *
13+ * Makes a remote HTTP request and returns a object repreenting the request.
14+ * Please note that this function does not support Proxies, Is not cached, And times out on the connection attempt after 3 seconds.
15+ *
16+ * The headers sent by this function can be modified/added to by the 'remote_http_headers' filter.
17+ * All headers returned by this function are normalised to lowercase, Content is not modified.
18+ *
19+ * @since 2.6
20+ *
21+ * @param string $url the URL to open
22+ * @param string $method (optional) the HTTP method for the connection, Accepts GET, POST or HEAD
23+ * @param string|array $data (optional) an Associative array containing data keys to be sent, or a pre-made HTTP query string
24+ * @param array $headers (optional) Extra headers to send with the connection. eg. array('Referer' => 'http://localhost/');
25+ * @return bool|object Returns false on failure, Else an object with 4 properties: "stauts", "statusname", "headers" and "content"
26+ */
27+function remote_http($url, $method='GET', $data='', $headers = array()){
28+       //Note: This returns all headers in lowercase.
29+       global $wp_version;
30+       
31+       if( ! function_exists('fsockopen') )
32+               return false;
33+       
34+       if( is_array($data) )
35+               $data = http_build_query($data);
36+
37+       if( ! is_array($headers) )
38+               $headers = array();
39+
40+       $allowed_methods = array('GET', 'POST', 'HEAD');
41+       if( ! in_array($method, $allowed_methods) )
42+               $method = 'GET';
43+
44+       $port = 80;
45+       $schema = 'http';
46+       $path = $host = $query = '';
47+       $parts = parse_url($url);
48+       extract($parts);
49+
50+       if( ! empty($query) )
51+               $query = "?$query";
52+       
53+       $http_request  = "$method $path$query HTTP/1.0\r\n";
54+       $http_request .= "Host: $host\r\n";
55+
56+       if( ! isset($headers['User-Agent']) )
57+               $headers['User-Agent'] = 'WordPress/' . $wp_version . '; ' . get_bloginfo('url');
58+
59+       if( ! empty($data) ) {
60+               $headers['Content-Length'] = strlen($data);
61+               
62+               if( ! isset($headers['Content-Type']) )
63+                       $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
64+       }
65+
66+       $headers = apply_filters('remote_http_headers', $headers);
67+       
68+       if( ! empty($headers) )
69+               foreach($headers as $header => $value)
70+                       $http_request .= "$header: $value\r\n";
71+
72+       $http_request .= "\r\n";
73+       if( ! empty($data) )
74+               $http_request .= $data;
75+
76+       $response = '';
77+       if( false == ( $fs = @fsockopen( $host, $port, $errno, $errstr, 3) ) || ! is_resource($fs) )
78+               return false;
79+
80+       fwrite($fs, $http_request);
81+
82+       while ( !feof($fs) )
83+               $response .= fgets($fs, 1160); // One TCP-IP packet
84+       fclose($fs);
85+       $response = explode("\r\n\r\n", $response, 2);
86+       
87+       $ret = (object)array('status' => 0, 'statusvalue' => '', 'headers' => array(), 'content'=> $response[1]);
88+
89+       foreach( explode("\n", $response[0]) as $rheader) {
90+               if( strpos($rheader, ':') ) { //Proper header
91+                       list($header, $value) = explode(':', $rheader, 2);
92+                       if( !empty($header) && ! empty($value) )
93+                               $ret->headers[ strtolower(trim($header)) ] = trim($value);
94+               } else if( strpos($rheader, 'HTTP') > -1) { //A Status header.
95+                       list(,$status, $status_value) = preg_split('|\s+|', $rheader,3);
96+                       $ret->status = trim($status);
97+                       $ret->statusvalue = trim($status_value);
98+               }
99+       }
100+
101+       return $ret;
102+}
103+}
104 ?>