Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not follow redirects when working with file_get_contents #138

Open
abeger opened this issue Aug 24, 2015 · 3 comments
Open

Does not follow redirects when working with file_get_contents #138

abeger opened this issue Aug 24, 2015 · 3 comments
Labels
Bug Something isn't working

Comments

@abeger
Copy link

abeger commented Aug 24, 2015

My test references a URL that turns out to be a 301 redirect. The code being tested uses file_get_contents and works fine outside of the test. When using PHP VCR, however, the fixture stored is just the 301 response. Is there some way to make it follow the redirect?

@adri
Copy link
Contributor

adri commented Sep 26, 2015

Hey @abeger, thank you. Could you please try the request again with a context, like in this example? Does that work?

$opts = array('http' =>
    array(
        'follow_location' => 1,
    )
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/', false, $context);

We check for follow_location already but in the PHP documentation it says it is 1 by default. I think we need set it by default too.

@abeger
Copy link
Author

abeger commented Nov 4, 2015

Interestingly, it followed the redirect with that, but included the header information in the returned content. I'm going to try to look into this a little more and see if I can't figure out where the disconnect is happening. Or at least create a reproducible example.

@abeger
Copy link
Author

abeger commented Nov 5, 2015

I ended up creating a little project to demonstrate the issue. You can look at it here: https://github.com/abeger/php-vcr-issue-138

High-level summary:

  • file_get_contents follows a 302 redirect just fine without specifying context.
  • A cassette wrapped around the call just records the 302 and doesn't follow the redirect. file_get_contents returns null.
  • Adding context to the file_get_contents follows the redirect, but the page's header information is prepended to the returned contents.

Let me know if you have any questions!

@adri adri added the Bug Something isn't working label Jan 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants