PHP调用Salesforce WebToLead时遇TCP connection reset by peer问题求助
Let's work through fixing this frustrating TCP connection reset error you're hitting when sending lead data to Salesforce's Web-to-Lead endpoint. Based on your code, tech stack (RHEL, Apache, PHP/cURL), and the lead data you shared, here are actionable changes to get things working again:
1. Replace Manual POST Data Encoding with http_build_query()
Your current code manually constructs the POST fields string, which is error-prone—special characters might not be encoded correctly, and small typos can break the request. Instead, use PHP's built-in http_build_query() function to handle encoding automatically:
// Delete your entire foreach loop that builds $strFields, and replace it with: $strFields = http_build_query($pStrPostArr);
This ensures all characters (like spaces, &, and special symbols in your lead data) are properly URL-encoded, matching what Salesforce expects. Manual encoding is a common source of malformed requests that trigger connection resets.
2. Enable SSL Certificate Verification (And Use Valid CA Certificates)
You currently disable CURLOPT_SSL_VERIFYPEER, which is insecure and can trigger Salesforce's security filters, leading to connection resets. Let's fix that by enabling verification and pointing cURL to RHEL's default CA certificate bundle:
// Replace this line: curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); // With these lines: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/certs/ca-bundle.crt'); // Default RHEL CA path
RHEL ships with a trusted CA bundle at that path—using it ensures cURL can validate Salesforce's SSL certificate correctly, avoiding security-related blocks.
3. Add Connection Timeouts and Retry Logic
TCP resets often happen due to temporary network blips or Salesforce's servers being temporarily busy. Adding timeouts and a simple retry loop can mitigate this:
// Add these timeout options to your cURL setup: curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 10s timeout to establish connection curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 30s total timeout for the full request // Wrap curl_exec in a retry loop (replace your existing curl_exec call with this) $maxRetries = 3; $retryCount = 0; $this->_strReponse = false; while ($retryCount < $maxRetries && $this->_strReponse === false) { $this->_strReponse = curl_exec($ch); if ($this->_strReponse === false) { $this->_strError = curl_error($ch); // Only retry on connection-related errors if (strpos($this->_strError, 'reset by peer') !== false || strpos($this->_strError, 'Connection timed out') !== false) { $retryCount++; sleep(1); // Wait 1 second before retrying } else { break; // Exit loop for non-retryable errors (like invalid data) } } }
This gives temporary issues a chance to resolve themselves without failing the lead submission entirely.
4. Fix Typos and Validate Required Parameters
I noticed a small typo in your code: $strDestiantionURL (extra 'i') and desitnationURL in your POST array. While you overwrite the URL in code, double-check that all required Salesforce parameters are present and valid:
- Ensure your
oid(Organization ID) is correct and active - Confirm all mandatory lead fields for your Salesforce setup are populated (even if some are empty strings, make sure they're allowed to be empty)
5. Check RHEL Network/Firewall Settings
Sometimes the issue isn't in code, but in your server's network configuration:
- Test connectivity from your RHEL server to Salesforce directly using terminal:
If this fails, your server's firewall (firewalld/iptables) might be blocking outbound HTTPS (port 443) traffic. Update your firewall rules to allow connections to Salesforce's domains.curl -v https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8 - If your server uses a proxy, add proxy settings to your cURL call:
curl_setopt($ch, CURLOPT_PROXY, 'your-proxy-host:port'); curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxy-user:proxy-pass'); // Only if required
Debugging Next Steps
If you still see errors after these changes, enable verbose cURL logging to get more details about the connection process:
curl_setopt($ch, CURLOPT_VERBOSE, true); $verboseLog = fopen('/tmp/curl_salesforce.log', 'w'); curl_setopt($ch, CURLOPT_STDERR, $verboseLog);
Check the log file (/tmp/curl_salesforce.log) for specific connection failures that can point you to the root cause.
内容的提问来源于stack exchange,提问作者Vipin Kumar R. Jaiswar

