Can't download page with file_get_contents

Issue Summary

I want to download a newly created page in my plugin, but I get the following error:

PHP Warning: file_get_contents(https://reactpress.local/test): failed to open stream: operation failed in /home/macco/Local Sites/reactpress/app/public/wp-content/plugins/reactpress/admin/class-reactpress-admin.php on line 305

The weird thing is, it works when I am using a PHP repl in a Site Shell.

Does anybody have an idea, what could be the problem?

Troubleshooting Questions

  • Does this happen for all sites in Local, or just one in particular?

It happens in all Local installations, it worked before in Local, but I can’t remember the version. Other dev and live environments don’t have this error.

System Details

  • Which version of Local is being used?

    • 6.1.1+5468
  • What Operating System (OS) and OS version is being used?

    • Ubuntu 20.04.01
  • Attach the Local Log. See this Community Forum post for instructions on how to do so:

Hey @rockiger, Welcome to the Local Community Forums!

I’ve had occasional trouble with file_get_contents() in Local too - usually it has to do with making a secure connection over HTTPS.

A couple of things to try:

  1. Convert the site back to using just regular HTTP within Local and try making the request over http://reactpress.local/test
  2. Explore using WordPress’ HTTP API, which often does a better job of abstracting that HTTPS handshake. In many cases, if I was going to use file_get_contents() I would use wp_remote_get() instead.

Thank that helped.

Using

wp_remote_retrieve_body(
        wp_remote_get(site_url() . '/' . $pageslug, ['timeout' => 1000])
);

did the trick. But only after I ramped up the timeout.

1 Like

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.