PHP Curl Does Not Work on Localhost?
When you see cURL support listed as enabled in your phpinfo() output, it is tempting to assume the library is fully operational. However, enabling the extension is distinct from configuring the environment to actually make requests. The discrepancy between a script working on a production host and failing locally usually points to one of three specific areas: network accessibility, SSL certificate verification, or DNS resolution.
The code snippet provided attempts to call the Google Geocoding API over HTTP. On a production server, this works because the external machine can route traffic through your public IP address. In a local development environment like MAMP Pro, the constraints are different. The most common failure mode involves SSL verification. Even though the request is made via http://, modern PHP versions and cURL configurations often default to strict SSL checks for security. If your local stack or the browser proxy used by MAMP presents an untrusted certificate chain, cURL will abort the connection immediately, returning a status code like SSL_ERROR_SSL or similar, rather than the expected JSON response.
You can diagnose this by inspecting the HTTP status code returned by curl_exec. A generic error message in your script logic might mask an underlying SSL failure. To bypass strict certificate verification temporarily for local testing, you would need to set the following option:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
While this allows the script to run on localhost, it introduces a security risk if this logic is ever deployed to production. It is better to ensure your local environment uses valid certificates or to explicitly handle SSL errors before falling back to insecure flags.
Another frequent culprit in local environments is DNS resolution. Your local machine might have specific hosts file entries that interfere with external lookups, or the network interface used by MAMP (often loopback) may not be configured to reach external gateways correctly depending on your firewall settings. Check your system's hosts file (/etc/hosts on macOS) for any entries mapping maps.googleapis.com to 127.0.0.1. If such an entry exists, it will cause the request to fail silently or return a connection refused error.
Furthermore, MAMP Pro runs PHP processes with specific permissions and network isolation. The phpinfo() output confirms the library is loaded, but it does not confirm that the underlying system libraries (like OpenSSL) are properly linked against the cURL binary being invoked. You can verify the actual cURL version and SSL backend by running:
curl --version
If this command returns a different version or lacks expected features compared to your PHP phpinfo() output, there is a mismatch in the shared libraries. On macOS, ensure that the OpenSSL library paths are correctly set in your environment variables if you are using a custom MAMP installation.
Finally, consider the protocol used. The example uses http://. While Google supports this, some network environments or corporate proxies might block unencrypted traffic to known API endpoints while allowing HTTPS. Switching to https:// is generally recommended for production and often resolves connectivity issues on localhost where the local stack prefers secure connections. If you switch to HTTPS, ensure your system clock is accurate; certificate validation will fail if the date on your Mac deviates significantly from real time.
If you are building a robust application, consider using a dedicated HTTP client like Guzzle or Laravel's built-in HttpClient facade. These libraries abstract away much of the manual cURL configuration and provide better error handling out of the box. For instance, Laravel's \Illuminate\Http\Client makes it trivial to inspect response content types and handle redirects automatically, reducing the likelihood of silent failures in your geocoding logic.