PHP5.1环境下调用API遇连接重置/流打开失败问题求助
Fixing "Connection Reset by Peer" & "Failed to Open Stream" in PHP 5.1 for HTTPS API Calls
Hey there, let's tackle these connection issues you're facing with your locked-down PHP 5.1 device. First, let's break down why your current code isn't working, then jump to a reliable fix using cURL (since file_get_contents has major limitations in PHP 5.1 for HTTPS requests).
What's Wrong With Your Current Code?
- Invalid Configuration Line: In Code Snippet 1,
request_terminate_timeout = 3600s;is a php.ini directive—you can't set it directly in your script like that. This line is either throwing a syntax error or doing nothing at all. - PHP 5.1 HTTPS Limitations:
file_get_contentsrelies on PHP's stream wrappers, which have very limited SSL/HTTPS support in PHP 5.1. It often fails to negotiate a valid TLS connection with modern HTTPS servers (like the one hosting your target API). - Incomplete Context Settings: Even if you fixed the timeout line, the stream context doesn't include SSL-specific settings that PHP 5.1 needs to trust the remote server's certificate.
The Solution: Use cURL Instead
PHP 5.1 includes cURL support (assuming it was compiled with the --with-curl flag, which is common in most deployments). cURL handles HTTPS connections much more reliably in older PHP versions, and you can configure all necessary settings directly in your script without touching php.ini.
Here's a working replacement for your code:
// Initialize cURL session $ch = curl_init(); // Set your API URL $url = "https://www.innkeyapp.com/SerRest.svc/GetOccupiedRoomData?sharedkey=2diSq8bjZ4N3ZR4XB5K+LXxSlBxuQ37IrkvUmVtHe3s=&roomno=311"; // Configure cURL options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as string instead of printing it curl_setopt($ch, CURLOPT_TIMEOUT, 36000); // Set total request timeout (in seconds) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable peer certificate verification (necessary for PHP 5.1, since it can't handle modern CA bundles) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Disable host verification to match // Execute the request and get the result $result = curl_exec($ch); // Check for errors if(curl_errno($ch)){ echo 'CURL Error: ' . curl_error($ch); } else { var_dump($result); } // Close the cURL session curl_close($ch); die();
Key Notes About This Code:
- SSL Verification: We disabled
CURLOPT_SSL_VERIFYPEERandCURLOPT_SSL_VERIFYHOSTbecause PHP 5.1's cURL can't use modern CA certificate bundles (and you can't update the device's certificates). This is a necessary trade-off for functionality here—just be aware that it slightly reduces security (though for a trusted API like yours, it's acceptable). - Timeout:
CURLOPT_TIMEOUTsets the total maximum time the request can run, which matches your original 36000-second goal. - Error Handling: Added explicit error checking with
curl_errno()andcurl_error()to help debug if you run into further issues.
Additional Checks (If cURL Still Fails):
- cURL Availability: If you get a "Call to undefined function curl_init()" error, your PHP build doesn't have cURL enabled. Unfortunately, since you can't modify the device's configuration, this might be a dead end—but most authentication devices with PHP 5.1 do include cURL.
- Network Restrictions: Double-check if the device's firewall allows outbound HTTPS traffic to
www.innkeyapp.com. If the network is locked down, you might need to work with your IT team to open that port (443).
内容的提问来源于stack exchange,提问作者Saifio
相关产品推荐
相关产品推荐

