PHP CURL works with HTTPS on local machine but fails on hosted site, HTTP works on hosted site, fails on local machine

I ran into conflicting behaviors using CURL with HTTP vs HTTPS. The curl code runs an email logging routine in the background.

On my local machine this works, but fails on the hosted site:

$curl_url = 'https://'.$_SERVER['HTTP_HOST'].'/email/_email_logging.php?log_id='.$log_id;

On the hosted site, this works, but fails on the local machine

$curl_url = 'http://'.$_SERVER['HTTP_HOST'].'/email/_email_logging.php?log_id='.$log_id;

Complete CURL code:

$curl_url = 'http://'.$_SERVER['HTTP_HOST'].'/email/_email_logging.php?log_id='.$log_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
curl_exec($ch);
curl_close($ch); 

An explanation of what could be happening would be appreciated and what tweak I could make to the CURL code to accept one of HTTP or HTTPS (preferred?) in both environments.

The conflicting behavior you’re experiencing with CURL using HTTP vs. HTTPS on your local and hosted environments can be attributed to several factors. Let’s break it down:

Possible Reasons for the Behavior:
SSL Certificate Issues (Local Environment):

On your local machine, if you’re using HTTPS and the local server doesn’t have a valid SSL certificate, CURL may fail when trying to connect via HTTPS.
However, since you’re setting CURLOPT_SSL_VERIFYPEER to false, it should bypass SSL verification, but other factors (like the server’s SSL configuration) could still cause issues.
Server Configuration Differences:

On your hosted site, the server is likely configured to redirect all HTTP requests to HTTPS. This means if you use HTTP, the server may automatically redirect it to HTTPS. This redirection might work differently depending on how your hosting provider handles it, leading to potential issues in one environment and not the other.
Firewalls or Security Rules:

Some hosted environments have strict security rules that might block HTTP requests or behave differently when interacting with non-HTTPS URLs. Similarly, your local environment might be less restrictive and allow HTTP without any issues.
URL Resolution and Host Headers:

Depending on how your local environment is set up (e.g., if you’re using a local development tool like XAMPP, WAMP, or MAMP), HTTP and HTTPS might resolve differently. There might be a mismatch in how the host headers are being sent or resolved.
Preferred Solution: Use HTTPS with Fallback
You generally want to use HTTPS whenever possible for secure communication. However, if there are issues with HTTPS on the local environment, you can implement a fallback to HTTP only when necessary.

Here’s how you can modify your CURL code to handle both environments:

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$curl_url = $protocol . $_SERVER['HTTP_HOST'] . '/email/_email_logging.php?log_id=' . $log_id;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);

if (curl_errno($ch)) {
    // Handle the error or log it
    error_log('CURL error: ' . curl_error($ch));
}

curl_close($ch);

Explanation of the Code:
Protocol Detection:

The $protocol variable automatically selects https:// if the server is using HTTPS; otherwise, it falls back to http://.
This approach ensures that CURL will use HTTPS on your hosted site (which is likely using SSL) and HTTP on your local machine if SSL isn’t configured.
Error Handling:

Added basic error logging using curl_errno() and curl_error() to help diagnose issues if CURL fails.

New contributor

mosh moshak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Adding this additional CURL line seems to have worked:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật