WordPress Plugin Development: How can I get the source code of a redirected page with wp_remote_get()?
-
I'm trying to get HTML source code of a page that is redirected from another url. localhost/redirects/index.php - redirects to localhost/redirected_page.php <meta http-equiv="refresh" content="0; url=http://localhost/redirected_page.php" /> localhost/redirected_page.php - just echos hello world. <?php echo 'hello world!'; Now, with wp_remote_get(), I'd like to retrieve the source code of the destination page. This only shows the contents of index.php. So how can I achieve this? <?php /* Plugin Name: Test WP Remote Redirect */ add_action( 'admin_menu', 'wp_remote_get_redirect_admin_menu' ); function wp_remote_get_redirect_admin_menu() { add_options_page( 'WP Remote Get Redirect', 'WP Remote Get Redirect', 'manage_options', basename( __FILE__, ".php" ), 'wp_remote_get_redirect_admin_page' ); } function wp_remote_get_redirect_admin_page() { ?> <div class="wrap"> <h1>WP Remote Get Redirect</h1> <?php $html = wp_remote_get('http://localhost/redirects/index.php'); echo htmlspecialchars( $html['body'] ); ?> </div> <?php }
-
Answer:
wp_remote_get only follows HTTP redirects (i.e. in the HTTP header) -- it wonât follow a meta-refresh or javascript based URL modification as it doesnât actually parse/render the content. To get the content of the destination page in this scenario, youâd need to check the HTML for a meta refresh tag, then remote_get the url in the content attribute -- recursively -- as the next page may also have a meta-refresh ;-) You can probably do that with strpos and a regex, but ideally youâd use a PHP HTML parser: http://htmlparsing.com/php.html Handling a JS based redirect is well... way too involved for a Quora answer. Good luck with that!
Nick Ciske at Quora Visit the source
Related Q & A:
- How can i get this music player on my myspace page?Best solution by Yahoo! Answers
- How can I get an overseas job as a civil engineer?Best solution by jobs.monster.com
- How can I get tickets for the serie a Torino-Juventus derby?Best solution by Yahoo! Answers
- How can I get my regular avatar of a real picture back as my avatar instead of the one now?Best solution by Yahoo! Answers
- How can I get AT&T Yahoo as a home page?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.