0

I want to open HTTPS file with PHP but this page makes a redirection to another page so fopen function doesn't parse the page I want.

I have this code :

$url = 'myHTMLPageWithParameters';

$file = file($url);

// test
var_dump($file);

And result :

array (size=12)
  0 => string '<html>
' (length=7)
  1 => string '<head>
' (length=7)
  2 => string '<script language="javascript">
' (length=31)
  3 => string 'function setTop(){top.location="/index.htm"}
' (length=45)
  4 => string '</script>
' (length=10)
  5 => string '<title>...</title>
' (length=19)
  6 => string '</head>
' (length=8)
  7 => string '
' (length=1)
  8 => string '<body onLoad="setTop()">
' (length=25)
  9 => string '</body>
' (length=8)
  10 => string '
' (length=1)
  11 => string '</html>
' (length=8)

When I display 'myHTMLPageWithParameters' in a HTML browser, I see the correct page after redirection. I'm just looking for a way to capture HTML code of the second page (after the redirection). Thanks for any help

Olivier J.
  • 3,115
  • 11
  • 48
  • 71
  • If there is no redirection via HTTP Location header, then there is of course nothing that fopen could automatically follow even if it wanted to. If the JS code is the only thing that “redirects” here, then you will have to parse and (pseudo-)execute that JS code to get the target address for the redirect. – CBroe Jan 21 '14 at 21:12

3 Answers3

1

Possible duplicate of follow redirects with curl in php

In short: it's not doable in a reliable manner.

This is not a redirection done by the server, you are getting the page that you requested. Then, that page redirects to another, but using javascript. Javascript it's interpreted by the browser, not by php, curl or any other library.

The only way I can think of, it's by using regex to find location.href or location.top and then follow those redirects. But again, there are plenty ways to redirect a page, you can't expect to parse them all!

Community
  • 1
  • 1
JohnKiller
  • 2,008
  • 18
  • 28
0

Check out this solution from another SO post:

Will PHPs fopen follow 301 redirects?

Another option would be to use curl instead of fopen, which has an option you can set telling it to follow redirects (CURLOPT_FOLLOWLOCATION).

Community
  • 1
  • 1
John McMahon
  • 1,605
  • 1
  • 16
  • 21
  • 1
    you could write it as comment. Its duplicate. – voodoo417 Jan 21 '14 at 21:11
  • Sorry, I just find solution. It was just because I forgot to send cookies to my context so there was a redirection because the HTTPS page didn't "recognized" me. With browser it works because it sends cookies naturally. Thanks anyway – Olivier J. Jan 21 '14 at 21:12
0

You can use FOLLOW_LOCATION;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "myHTMLPageWithParameters");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$html_response = curl_exec($ch);

// We get the content
$html = str_get_html($html_response);

// Get #result div for example
$content = $html->find('#result');
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73