Curl error 60, SSL certificate issue: self signed certificate in certificate chain
When you attempt to fetch an access token from a third-party OAuth endpoint using cURL, encountering error 60 is a common stumbling block if the target service has not yet transitioned to standard public CA-signed certificates. In your specific scenario, accessing https://oauth.vk.com/access_token works perfectly in a browser but fails with curl_error() returning "self signed certificate in certificate chain." The discrepancy lies in how browsers and cURL handle SSL validation by default.
Browsers, like Chrome or Firefox, are often configured to be lenient toward self-signed certificates, especially during initial development or when interacting with known test environments. They might display a warning but still allow the connection to proceed, enabling you to see the access_token in the response. Conversely, cURL defaults to strict SSL verification (CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST are enabled by default). It refuses to proceed if the server's certificate cannot be traced back to a trusted Certificate Authority (CA) or if it is self-signed without a valid chain of trust.
To resolve this immediately for your script, you must explicitly tell cURL to skip peer verification. This is achieved by setting CURLOPT_SSL_VERIFYPEER to false. While this allows the request to succeed and retrieve the token, it introduces significant security risks if you are handling sensitive data or production secrets. You should only use this approach for local development or temporary fixes.
Here is how you modify your existing cURL code to bypass the error:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, 'https://oauth.vk.com/access_token');
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Disable SSL certificate verification (BYPASS ERROR 60)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// $output contains the output string
$output = curl_exec($ch);
if ($output === false) {
echo 'Error: ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
// Process $output to extract access_token
print_r(json_decode($output));
}
// close curl resource to free up system resources
curl_close($ch);
By adding CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST, you effectively tell cURL: "Trust whatever certificate the server sends, even if it is self-signed." This aligns cURL's behavior with your browser's permissive stance.
However, there is a better long-term solution. The underlying issue indicates that the VK OAuth endpoint is currently serving a self-signed or improperly chained certificate. If you are building an application intended for production, relying on disabled SSL verification is never sustainable. You should investigate whether this is a temporary state of the API or if they are in the process of migrating their infrastructure.
If you need to interact with such endpoints persistently, you can create a custom CA bundle containing the root certificate that signs the VK endpoint. You would then pass this bundle to cURL using CURLOPT_CAPATH or CURLOPT_CAINFO. This approach maintains SSL verification integrity while trusting your specific authority.
Alternatively, if this is strictly an internal development tool and not handling user secrets, consider using a local reverse proxy (like Ngrok or a local Nginx instance) that terminates the SSL connection with a valid certificate forwarded to your application logic. This keeps your backend code secure while allowing it to communicate with external services over HTTP or through your own trusted HTTPS gateway.
Remember that disabling peer verification exposes your application to Man-in-the-Middle (MitM) attacks. An attacker on the same network could intercept the request, modify the response, or inject malicious tokens into the access_token string before it reaches your PHP script. Always weigh the convenience of skipping certificate checks against the security implications, especially when using credentials like APP_SECRET.