Description
Latest status
Curl and tcp over fetch() are now a part of WordPress Playground 🎉 Here's what we still need to close this issue:
- CORS: Use CORS proxy for all CORS-affected outbound requests #1940
- Enable networking by default #1225
Description
WordPress Playground only has a partial support for network calls.
Types of network calls in WordPress
wp_safe_remote_get
As of #724, Playground is capable of translating wp_safe_remote_get
calls into JavaScript fetch()
requests. This has limitations:
- Only https:// URLs are supported
- The server must provide valid CORS headers in the response
- Developers can't control all the headers
Arbitrary network calls
Other methods of accessing the network, such as libcurl
or file_get_contents
, are not supported yet.
Web browsers do not allow the WebAssembly code to access the internet directly yet. A native socket API may or may not be released in the future, but there isn't one for now. #1093 would improve the situation.
In Node.js, Playground access the network using the following method:
- Set up a same-domain API endpoint that accepts network commands from the browser
- Capture socket function calls in the WebAssembly binary
- Pass them to JavaScript
- Pass the requested operation over the API endpoint using the
fetch()
orWebSocket
This may not be viable on the web as someone would have to pay for the hardware to run the proxy on, and the proxy's nature mean there are security risks related to accessing the local network.
Solution
After 1,5 years of exploring and discussing, this issue finally has a path forward:
- Merge PHP: Support http:// https:// and ssl:// stream wrappers #1093
- Merge Curl extension for the Node.js build of PHP.wasm #1273
- Ship curl in the browser
- This would enable requesting all CORS-enabled HTTPS endpoints via
file_get_contents
,curl_exec
etc. and without going throughwp_safe_remote_get()
.
For full networking support, we'd also need the following:
- Expose the Node networking proxy as a separate, runnable script
- Provide an API to connect it to the in-browser version Playground
- Document the workflow
Nice to haves:
- Ship a version of the network built in PHP script to enable running a full-featured Playground build in the same environments as WordPress.
- Provide a Dockerfile to set up the network proxy and a few buttons for quickly spinning proxy cloud nodes on, e.g. CloudFlare, Digital Ocean, etc.
Limitations of the approach
Limitations without the network proxy:
- Non-CORS URLs wouldn't work
- Non-HTTPS traffic wouldn't work
- gethostname and other low-level methods still wouldn't work
- SSL certificate checks, like the ones done by Composer, wouldn't work
All of the above could be resolved by plugging in a network proxy.