Working with cURL, WordPress, and Valet

I’m one of those types who uses the 1.1.1.1 and 1.0.0.1 as my primary DNS servers.

But one problem that comes with using these as my main DNS servers (and maybe others, I don’t know as I’ve not tried them) is that when I’m using cURL to make requests to a third-party server, it will often result in an a 404.

Luckily, there’s an easy fix.

cURL, WordPress, and Valet

In short, the simple change is to add your local host as a DNS entry to your network preferences.

If you’re interested in seeing the steps necessary to do this and some example code that won’t work (before the change, of course), then read on.

Note that I’m going to be using screenshots for this but if you’re comfortable editing DNS files on your native operating system, it will work just as well.

Updating DNS

Depending on the operating system you’re using, how you arrive at changing up the DNS settings will vary.

If you’re using macOS, then the Network tab and the Advanced options for whatever adapter to use to connect to the Internet will provide you with what you need to update.

On that screen, click on the DNS tab then add 127.0.0.1 to the top of your list.

Sample Code

When it comes to seeing the reason behind needing to do something like this, I’ve always found seeing sample code to be useful. The main reasons is it it gives us a something to test before making the above changes and then after making the above changes.

With that said, here’s a bit of code I was using that doesn’t work without the proper DNS set up above:

But once the DNS has been updated, this works just fine. And, to be clear, I still advocate for wp_safe_remote_get and wp_safe_remote_post but that nature of projects will also dictate what constraints we have.

Furthermore, take a peak at those functions, if you have a chance, to see what’s happening behind the scenes. 🙂

That’s All

Like I mentioned earlier, it’s an easy process. I’ve found it useful to share both what kind of code may be causing the problem, though, so if you see something similar and are getting a similar result with using cURL, WordPress, and Valet, then you know how to solve it.