Make WordPress Core

Changeset 9013


Ignore:
Timestamp:
09/27/2008 09:41:19 PM (16 years ago)
Author:
westi
Message:

Move the old simple http method to use the new HTTP api. See #7793 props jacobsantos.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r8972 r9013  
    10221022 * @since 2.5.0
    10231023 *
    1024  * @param string $url
     1024 * @param string $url URL to fetch.
    10251025 * @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.
    10271027 * @return bool|string False on failure and string of headers if HEAD request.
    10281028 */
    1029 function wp_get_http( $url, $file_path = false, $red = 1 ) {
    1030     global $wp_version;
     1029function wp_get_http( $url, $file_path = false, $deprecated = false ) {
    10311030    @set_time_limit( 60 );
    10321031
    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';
    10441037    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 )
    10791044        return $headers;
    1080     }
    10811045
    10821046    // 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'] );
    10841048    $got_bytes = 0;
    10851049    $out_fp = fopen($file_path, 'w');
     
    10941058
    10951059    fclose($out_fp);
    1096     fclose($fp);
     1060
    10971061    return $headers;
    10981062}
     
    11041068 *
    11051069 * @param string $url
    1106  * @param int $red Optional. Number of redirects.
     1070 * @param bool $deprecated Not Used.
    11071071 * @return bool|string False on failure, headers on success.
    11081072 */
    1109 function wp_get_http_headers( $url, $red = 1 ) {
    1110     return wp_get_http( $url, false, $red );
     1073function wp_get_http_headers( $url, $deprecated = false ) {
     1074    $response = wp_remote_head( $url );
     1075    return wp_remote_retrieve_headers( $response );
    11111076}
    11121077
     
    12721237 * HTTP request for URI to retrieve content.
    12731238 *
    1274  * Tries to retrieve the HTTP content with fopen first and then using cURL, if
    1275  * fopen can't be used.
    1276  *
    12771239 * @since 1.5.1
     1240 * @uses wp_remote_get()
    12781241 *
    12791242 * @param string $uri URI/URL of web page to retrieve.
     
    12811244 */
    12821245function 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.
    12841248    $parsed_url = @parse_url( $uri );
    12851249
     
    12871251        return false;
    12881252
    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'];
    13151259}
    13161260
Note: See TracChangeset for help on using the changeset viewer.