I have a website in local development at test:8888 and I am trying to get the following to work in my functions.php file.
$response = wp_remote_get( 'test:8888/?p=1' );
print_r($response);
Unfortunately this is printing
WP_Error Object ( [errors] => Array ( [http_request_failed] => Array ( [0] => A valid URL was not provided. ) ) [error_data] => Array ( ) )
Is it possible to request your own url while developing on localhost?
Ad Timeout
You should be able to get around the timeout using a filter
Choose the right protocol/scheme
About your protocol/scheme problem: If it’s your local install, you can use the conditional.
If you’re doing requests to your own local server, then there might be a problem with the SSL verification. WP uses a filter to trigger it’s setting:
This filter does not(!) trigger for local servers doing requests to a server on the web. For external servers use the following filter:
Issues with local requests
Then there also can be issues with blocked local request:
Other things that can be influencing your ability to do local requests are constants set wrong in your
wp-config.php
file:In case you’re using Proxies:
No cURL?
If there’s
cURL
not available and you’re not streaming data to a file, WP will fallback to usingFsocketopen()
instead. From a core comment, which sums it up nicely:Point is, that WP only jumps in if we got the host name
localhost
. The fsocketopen host will then get set to'127.0.0.1'
.I had a similar problem and solved it like this:
‘test:8888/?p=1’ isn’t a valid URL.
Try ‘http://test:8888/?p=1‘ instead.
I think you actually have two problems here. Otto’s answer solved your first problem, because leaving the
http://
off made it an invalid URL, which is why you got theA valid URL was not provided
error.The second problem is something different and unrelated, and is giving you the
Operation timed out
error. I can’t say for sure without seeing your entire code, but I’m guessing that thewp_remote_get()
call is inside a callback function which is registered to a hook that is also firing on the page that’s being requested bywp_remote_get()
. That situation creates a recursive loop that ultimately times out.You need to make sure that
wp_remote_get()
call isn’t being fired on the page that you’re requesting. You can do that by registering the callback to a different hook, or by using conditional tags to avoid callingwp_remote_get()
on the requested page.Here’s an example, assuming you’ll always be calling non-admin pages: