How to fix SalesForce authentication errors on localhost?
In general, to make SalesForce authentication to work on localhost, you need to:


Create an app in Salesforce with URL Callback to your local (https://localhost/salesforce/oauth_callback) with appropriate permissions.


  In Salesforce go to Your Name > Setup > Create > Apps then create a new Connected App. (Depending on your Salesforce instance, you may need to go to Your Name > Setup > Develop > Remote Access.)

Your site needs to be SSL enabled to authorize the remote application using OAUTH.
If using the SOAP API, PHP must be compiled with SOAP web services and OpenSSL support.
Use the right auth key and secret to Authorize your app (Configuration » Salesforce).
Make sure you're connecting to the right endpoints (live or sandbox).
For more help, check Help » Salesforce API in Drupal, or SalesForce documentation.
Add $conf['https'] = TRUE; to your setting file.


The error in Salesforce-&gt;requestToken() indicates that there is a problem exchanging an authorization code for an access token.

Further more, the error:


  Error opening socket ssl://login.salesforce.com:443 in Salesforce->requestToken()


indicates the problem connecting to Salesforce endpoint it-self (check requestToken() in includes/salesforce.inc for more details).

To diagnose the problem, please check Reports » Status report if you've got HTTP Failure error and that your localhost can connect to Salesforce end point via curl:

$ curl login.salesforce.com:443


If your curl is connecting fine and you've still HTTP Failure error in your Status report, then try to:


Temporary disable Secure Site or Secure Pages modules if present.
Make sure you've firewall (e.g. iptables) and antivirus is switched off (for testing purposes).
Make sure you're not behind the proxy.
Make sure you're not running LAMP in a chroot/jail/VM environment.
Make sure you some network functions aren't disabled PHP functions (e.g. stream_socket_client).
Make sure your PHP is not in Safe mode (safe_mode = On) by:

php -r "var_dump(ini_get('safe_mode'));"

Double check your /etc/hosts or ~/.curlrc doesn't have anything suspicious (you can add insecure to your .curlrc for testing purposes).
Check is there is no any problem with certificates (e.g. your system date is incorrect, you added the certificate and your PHP openssl sees your certificates correctly).

In PHP 5.6 you can specify openssl.cafile and openssl.capath,

e.g. openssl.cafile=/usr/local/etc/openssl/cert.pem


If none of above work, please check below simple PHP script which can check if your PHP can connect to external host:

&lt;?php
fsockopen('ssl://login.salesforce.com',443,$errno,$errstr) ? print("UP") : print("DOWN:".$errstr);
?&gt;


Save it to the file (test.php) and place it inside your web root and open via web browser.

If it fails, it's either problem with your PHP configuration or your network. You can debug it further more by using system call tracer (debugger) as below (install it if required):

$ strace -f -e network php test.php # For Linux
$ truss -f php test.php # For BSD
$ sudo dtruss php test.php # For OSX
# For Windows, you may look for StraceNT.


Then you've to analyse the log for any issues.

Or the following perl script:

use strict;
use warnings;

use LWP::UserAgent;
use Mozilla::CA;
my $ua = LWP::UserAgent-&gt;new(
        ssl_opts =&gt; {
                verify_hostname =&gt; 1,
                SSL_ca_file =&gt; Mozilla::CA::SSL_ca_file()
        }) or die;
my $url = $ARGV[0] || 'https://login.salesforce.com/';
my $res = $ua-&gt;get($url);
print $res-&gt;code . "\n";

exit;


See also:


Using cURL in PHP to access HTTPS (SSL/TLS) protected sites.
SSL Certificate Verification
Using Curl to login to Salesforce API at Salesforce Developers
token failed with status 0! at Salesforce Developers
How to fix Curl Error Ssl Certificate Problem Verify That The Ca Error?