当前位置: 首页>>代码示例>>PHP>>正文


PHP WP_Http::handle_redirects方法代码示例

本文整理汇总了PHP中WP_Http::handle_redirects方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_Http::handle_redirects方法的具体用法?PHP WP_Http::handle_redirects怎么用?PHP WP_Http::handle_redirects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WP_Http的用法示例。


在下文中一共展示了WP_Http::handle_redirects方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: request


//.........这里部分代码省略.........
     }
     if (is_array($r['headers'])) {
         foreach ((array) $r['headers'] as $header => $headerValue) {
             $strHeaders .= $header . ': ' . $headerValue . "\r\n";
         }
     } else {
         $strHeaders .= $r['headers'];
     }
     if ($proxy->use_authentication()) {
         $strHeaders .= $proxy->authentication_header() . "\r\n";
     }
     $strHeaders .= "\r\n";
     if (!is_null($r['body'])) {
         $strHeaders .= $r['body'];
     }
     fwrite($handle, $strHeaders);
     if (!$r['blocking']) {
         stream_set_blocking($handle, 0);
         fclose($handle);
         return array('headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array());
     }
     $strResponse = '';
     $bodyStarted = false;
     $keep_reading = true;
     $block_size = 4096;
     if (isset($r['limit_response_size'])) {
         $block_size = min($block_size, $r['limit_response_size']);
     }
     // If streaming to a file setup the file handle.
     if ($r['stream']) {
         if (!WP_DEBUG) {
             $stream_handle = @fopen($r['filename'], 'w+');
         } else {
             $stream_handle = fopen($r['filename'], 'w+');
         }
         if (!$stream_handle) {
             return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $r['filename']));
         }
         $bytes_written = 0;
         while (!feof($handle) && $keep_reading) {
             $block = fread($handle, $block_size);
             if (!$bodyStarted) {
                 $strResponse .= $block;
                 if (strpos($strResponse, "\r\n\r\n")) {
                     $process = WP_Http::processResponse($strResponse);
                     $bodyStarted = true;
                     $block = $process['body'];
                     unset($strResponse);
                     $process['body'] = '';
                 }
             }
             $this_block_size = strlen($block);
             if (isset($r['limit_response_size']) && $bytes_written + $this_block_size > $r['limit_response_size']) {
                 $this_block_size = $r['limit_response_size'] - $bytes_written;
                 $block = substr($block, 0, $this_block_size);
             }
             $bytes_written_to_file = fwrite($stream_handle, $block);
             if ($bytes_written_to_file != $this_block_size) {
                 fclose($handle);
                 fclose($stream_handle);
                 return new WP_Error('http_request_failed', __('Failed to write request to temporary file.'));
             }
             $bytes_written += $bytes_written_to_file;
             $keep_reading = !isset($r['limit_response_size']) || $bytes_written < $r['limit_response_size'];
         }
         fclose($stream_handle);
     } else {
         $header_length = 0;
         while (!feof($handle) && $keep_reading) {
             $block = fread($handle, $block_size);
             $strResponse .= $block;
             if (!$bodyStarted && strpos($strResponse, "\r\n\r\n")) {
                 $header_length = strpos($strResponse, "\r\n\r\n") + 4;
                 $bodyStarted = true;
             }
             $keep_reading = !$bodyStarted || !isset($r['limit_response_size']) || strlen($strResponse) < $header_length + $r['limit_response_size'];
         }
         $process = WP_Http::processResponse($strResponse);
         unset($strResponse);
     }
     fclose($handle);
     $arrHeaders = WP_Http::processHeaders($process['headers'], $url);
     $response = array('headers' => $arrHeaders['headers'], 'body' => null, 'response' => $arrHeaders['response'], 'cookies' => $arrHeaders['cookies'], 'filename' => $r['filename']);
     // Handle redirects.
     if (false !== ($redirect_response = WP_Http::handle_redirects($url, $r, $response))) {
         return $redirect_response;
     }
     // If the body was chunk encoded, then decode it.
     if (!empty($process['body']) && isset($arrHeaders['headers']['transfer-encoding']) && 'chunked' == $arrHeaders['headers']['transfer-encoding']) {
         $process['body'] = WP_Http::chunkTransferDecode($process['body']);
     }
     if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers'])) {
         $process['body'] = WP_Http_Encoding::decompress($process['body']);
     }
     if (isset($r['limit_response_size']) && strlen($process['body']) > $r['limit_response_size']) {
         $process['body'] = substr($process['body'], 0, $r['limit_response_size']);
     }
     $response['body'] = $process['body'];
     return $response;
 }
开发者ID:qaryas,项目名称:qaryas_site,代码行数:101,代码来源:class-wp-http-streams.php

示例2: update_data

function update_data()
{
    remove_filter('pre_post_title', 'pre_post_title');
    $upload_dir = wp_upload_dir();
    $filename = $upload_dir['basedir'] . '/data.json';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    $items = array();
    $args = array('numberposts' => -1, 'post_type' => 'business');
    foreach (get_posts($args) as $post) {
        $custom_fields = get_post_custom($post->ID);
        list($address) = $custom_fields['address'];
        list($latlng) = $custom_fields['latlng'];
        list($phone) = $custom_fields['phone'];
        $item = array('label' => my_html_entity_decode(wptexturize($post->post_title)), 'permalink' => get_permalink($post));
        $item += array_filter(compact('address', 'latlng', 'phone'));
        $website = esc_url_raw($custom_fields['website'][0]);
        if ($website) {
            $website = strtolower($website);
            $website = preg_replace('/^https?:\\/\\/(?:www\\.)?([-.\\d_a-z~]+(?:\\/[-.\\/\\d_a-z~]+)?)\\/*/', 'http://$1', $website);
            $args = array('timeout' => 300, 'redirection' => 0);
            $response = wp_safe_remote_get($website, $args);
            $args['redirection'] = 1;
            $args['reject_unsafe_urls'] = true;
            for ($i = 0; $i < 5; $i++) {
                if (is_wp_error($response)) {
                    delete_post_meta($post->ID, 'website_up');
                    break;
                }
                $redirect_response = WP_Http::handle_redirects($website, $args, $response);
                if (!$redirect_response) {
                    $website = preg_replace('/^(https?:\\/\\/[-.\\d_a-z~]+)\\/+$/i', '$1', $website);
                    //update_post_meta($post->ID, 'website', $website);
                    update_post_meta($post->ID, 'website_up', $website);
                    $body = wp_remote_retrieve_body($response);
                    $doc = new DOMDocument();
                    @$doc->loadHTML($body);
                    $url_parts = my_parse_url($website);
                    $urls = array();
                    foreach ($doc->getElementsByTagName('img') as $node) {
                        $relative_url_parts = my_parse_url($node->getAttribute('src'));
                        $relative_url_parts['path'] = str_replace(' ', '%20', $relative_url_parts['path']);
                        $relative_url_parts['query'] = str_replace(' ', '+', $relative_url_parts['query']);
                        if (!$relative_url_parts['scheme']) {
                            $relative_url_parts['scheme'] = $url_parts['scheme'];
                            if (!$relative_url_parts['host']) {
                                $relative_url_parts['host'] = $url_parts['host'];
                                $relative_url_parts['port'] = $url_parts['port'];
                                if (!$relative_url_parts['path']) {
                                    $relative_url_parts['path'] = $url_parts['path'];
                                    if (!$relative_url_parts['query']) {
                                        $relative_url_parts['query'] = $url_parts['query'];
                                    }
                                } else {
                                    if ($relative_url_parts['path'][0] != '/') {
                                        $relative_url_parts['path'] = "/{$relative_url_parts['path']}";
                                        if ($url_parts['path']) {
                                            $relative_url_parts['path'] = substr($url_parts['path'], 0, strrpos($url_parts['path'], '/')) . $relative_url_parts['path'];
                                        }
                                    }
                                }
                            }
                        }
                        do {
                            $relative_url_parts['path'] = preg_replace('/[^\\/]+\\/\\.\\.(?:\\/|$)/', '', $relative_url_parts['path'], 1, $count);
                        } while ($count);
                        $url = "{$relative_url_parts['scheme']}://{$relative_url_parts['host']}";
                        if ($relative_url_parts['port']) {
                            $url .= ":{$relative_url_parts['port']}";
                        }
                        $url .= $relative_url_parts['path'];
                        if ($relative_url_parts['query']) {
                            $url .= "?{$relative_url_parts['query']}";
                        }
                        $urls[$url] = $url;
                    }
                    if ($urls) {
                        $args = array('post_parent' => $post->ID, 'post_type' => 'attachment');
                        foreach (get_children($args) as $attachment) {
                            wp_delete_attachment($attachment->ID);
                        }
                        $found = false;
                        foreach ($urls as $url) {
                            $tmp_name = download_url($url);
                            list($width, $height) = getimagesize($tmp_name);
                            if ($width > 40 && $height > 40 && $width + $height > 180) {
                                preg_match('/[^\\/]+?(?=\\/?(?:$|\\?))/', $url, $matches);
                                $file_array = array('name' => $matches[0], 'tmp_name' => $tmp_name);
                                $thumbnail_id = media_handle_sideload($file_array, $post->ID);
                                if (!$found) {
                                    set_post_thumbnail($post, $thumbnail_id);
                                    $found = true;
                                }
                            }
                        }
                    }
                    break;
                }
                $website = WP_Http::make_absolute_url(wp_remote_retrieve_header($response, 'location'), $website);
//.........这里部分代码省略.........
开发者ID:jablko,项目名称:business,代码行数:101,代码来源:business.php


注:本文中的WP_Http::handle_redirects方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。