How do I retrieve a URL in Java?

WordPress Plugin Development: How do I retrieve the destination url of a redirected page with wp_remote_head()?

  • The following function returns the url of a redirected page. function get_destination_url_curl( $url ) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url ); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $a = curl_exec($ch); curl_close($ch); if (preg_match('#Location: (.*)#', $a, $r)) return trim($r[1]); } I'm wondering if the WordPress http://codex.wordpress.org/HTTP_API can handle it. I tried the following code but the returned value of array[headers][location] is always empty. $args = array( 'redirection' => 0, ); $array_response = wp_remote_head( $url, $args ); echo '<pre>' . print_r( $array_response, true ) . '</pre>'

  • Answer:

    wp_remote_head() should return a 'location' index in the resulting array... $result = wp_remote_head( $your_url, $your_args ); echo $result['headers']['location'];

Russell Heimlich at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.