Changeset 9013
- Timestamp:
- 09/27/2008 09:41:19 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/functions.php
r8972 r9013 1022 1022 * @since 2.5.0 1023 1023 * 1024 * @param string $url 1024 * @param string $url URL to fetch. 1025 1025 * @param string|bool $file_path Optional. File path to write request to. 1026 * @param int $red Optional. Number of Redirects. Stops at 5 redirects.1026 * @param bool $deprecated Deprecated. Not used. 1027 1027 * @return bool|string False on failure and string of headers if HEAD request. 1028 1028 */ 1029 function wp_get_http( $url, $file_path = false, $red = 1 ) { 1030 global $wp_version; 1029 function wp_get_http( $url, $file_path = false, $deprecated = false ) { 1031 1030 @set_time_limit( 60 ); 1032 1031 1033 if ( $red > 5 ) 1034 return false; 1035 1036 $parts = parse_url( $url ); 1037 $file = $parts['path'] . ( ( $parts['query'] ) ? '?' . $parts['query'] : '' ); 1038 $host = $parts['host']; 1039 if ( !isset( $parts['port'] ) ) 1040 $parts['port'] = 80; 1041 1042 if ( $file_path ) 1043 $request_type = 'GET'; 1032 $options = array(); 1033 $options['redirection'] = 5; 1034 1035 if ( false == $file_path ) 1036 $options['method'] = 'HEAD'; 1044 1037 else 1045 $request_type = 'HEAD'; 1046 1047 $head = "$request_type $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n"; 1048 1049 $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3 ); 1050 if ( !$fp ) 1051 return false; 1052 1053 $response = ''; 1054 fputs( $fp, $head ); 1055 while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false ) 1056 $response .= fgets( $fp, 2048 ); 1057 preg_match_all( '/(.*?): (.*)\r/', $response, $matches ); 1058 $count = count( $matches[1] ); 1059 for ( $i = 0; $i < $count; $i++ ) { 1060 $key = strtolower( $matches[1][$i] ); 1061 $headers["$key"] = $matches[2][$i]; 1062 } 1063 1064 preg_match( '/.*([0-9]{3}).*/', $response, $return ); 1065 $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404 1066 1067 $code = $headers['response']; 1068 if ( ( '302' == $code || '301' == $code ) && isset( $headers['location'] ) ) { 1069 fclose($fp); 1070 return wp_get_http( $headers['location'], $file_path, ++$red ); 1071 } 1072 1073 // make a note of the final location, so the caller can tell if we were redirected or not 1074 $headers['x-final-location'] = $url; 1075 1076 // HEAD request only 1077 if ( !$file_path ) { 1078 fclose($fp); 1038 $options['method'] = 'GET'; 1039 1040 $response = wp_remote_request($url, $options); 1041 1042 $headers = wp_remote_retrieve_headers( $response ); 1043 if ( false == $file_path ) 1079 1044 return $headers; 1080 }1081 1045 1082 1046 // GET request - fetch and write it to the supplied filename 1083 $content_length = $headers['content-length'];1047 $content_length = isset( $headers['content-length'] ) ? $headers['content-length'] : strlen( $response['body'] ); 1084 1048 $got_bytes = 0; 1085 1049 $out_fp = fopen($file_path, 'w'); … … 1094 1058 1095 1059 fclose($out_fp); 1096 fclose($fp); 1060 1097 1061 return $headers; 1098 1062 } … … 1104 1068 * 1105 1069 * @param string $url 1106 * @param int $red Optional. Number of redirects.1070 * @param bool $deprecated Not Used. 1107 1071 * @return bool|string False on failure, headers on success. 1108 1072 */ 1109 function wp_get_http_headers( $url, $red = 1 ) { 1110 return wp_get_http( $url, false, $red ); 1073 function wp_get_http_headers( $url, $deprecated = false ) { 1074 $response = wp_remote_head( $url ); 1075 return wp_remote_retrieve_headers( $response ); 1111 1076 } 1112 1077 … … 1272 1237 * HTTP request for URI to retrieve content. 1273 1238 * 1274 * Tries to retrieve the HTTP content with fopen first and then using cURL, if1275 * fopen can't be used.1276 *1277 1239 * @since 1.5.1 1240 * @uses wp_remote_get() 1278 1241 * 1279 1242 * @param string $uri URI/URL of web page to retrieve. … … 1281 1244 */ 1282 1245 function wp_remote_fopen( $uri ) { 1283 $timeout = 10; 1246 // parse url() should not be used for validation of URLs. 1247 // Keeping anyway, since the Filter extension is not available on all servers. 1284 1248 $parsed_url = @parse_url( $uri ); 1285 1249 … … 1287 1251 return false; 1288 1252 1289 if ( !isset( $parsed_url['scheme'] ) || !in_array( $parsed_url['scheme'], array( 'http','https' ) ) ) 1290 $uri = 'http://' . $uri; 1291 1292 if ( ini_get( 'allow_url_fopen' ) ) { 1293 $fp = @fopen( $uri, 'r' ); 1294 if ( !$fp ) 1295 return false; 1296 1297 //stream_set_timeout($fp, $timeout); // Requires php 4.3 1298 $linea = ''; 1299 while ( $remote_read = fread( $fp, 4096 ) ) 1300 $linea .= $remote_read; 1301 fclose( $fp ); 1302 return $linea; 1303 } elseif ( function_exists( 'curl_init' ) ) { 1304 $handle = curl_init(); 1305 curl_setopt( $handle, CURLOPT_URL, $uri); 1306 curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 1 ); 1307 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 ); 1308 curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout ); 1309 $buffer = curl_exec( $handle ); 1310 curl_close( $handle ); 1311 return $buffer; 1312 } else { 1313 return false; 1314 } 1253 $options = array(); 1254 $options['timeout'] = 10; 1255 1256 $response = wp_remote_get( $uri, $options ); 1257 1258 return $response['body']; 1315 1259 } 1316 1260
Note: See TracChangeset
for help on using the changeset viewer.