[wp-trac] [WordPress Trac] #43329: download_url function should return the body in response on error

WordPress Trac noreply at wordpress.org
Sat Mar 3 18:51:15 UTC 2018


#43329: download_url function should return the body in response on error
-------------------------+--------------------
 Reporter:  nextendweb   |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  5.0
Component:  HTTP API     |     Version:  4.9.4
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+--------------------

Comment (by nextendweb):

 I was able to find the solution for my issue without this modification, so
 this feature is not required for me. But I'm think it makes the HTTP api
 better.

 My solution was:
 {{{#!php
 <?php
     add_filter('upgrader_pre_download', 'n_upgrader_pre_download', 10, 3);

     n_upgrader_pre_download($reply, $package, $upgrader) {
         $needle = my_url_to_match();
         if (substr($package, 0, strlen($needle)) == $needle) {
             add_filter('http_response', 'n_http_response', 10, 3);
         }

         return $reply;
     }

     function n_http_response($response, $r, $url) {

         $needle = my_url_to_match();
         if (substr($url, 0, strlen($needle)) == $needle && (200 !=
 wp_remote_retrieve_response_code($response) || is_wp_error($response))) {

             if (isset($response['filename']) &&
 file_exists($response['filename'])) {

                 $body = @file_get_contents($response['filename']);
                 // Here I have the body of the error response

             }
         }

         return $response;
     }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43329#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list