|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-02-20 16:10 UTC] icez at icez dot net
Description: ------------ I have the code that call several servers behind the load balancer to check for the page load result. The server was configured with SSL so I need the way to force curl to connect to specific IP address with the correct hostname without having to set SSL_VERIFYHOST to false and found curl option CURLOPT_RESOLVE. I've tested that curl command line can use this option to force connect to specific server IP address, but when I run the test script in PHP it always connect to first server. which is the first address to have this option set to. related question: http://stackoverflow.com/questions/36434049/php-curl-curlopt-resolve-not-working Test script: --------------- $servers = [ '192.0.2.1', '192.0.2.2', '192.0.2.3' ]; foreach ($servers as $server) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 0); curl_setopt($ch, CURLOPT_RESOLVE, $resolveparam); curl_setopt($ch, CURLOPT_URL, "https://example.com/some/path"); curl_setopt($ch, CURLOPT_VERBOSE, 1); $result = curl_exec($ch); $info = curl_getinfo($ch); echo $info['primary_ip']."\n"; curl_close($ch); } Expected result: ---------------- 192.0.2.1 192.0.2.2 192.0.2.3 Actual result: -------------- 192.0.2.1 192.0.2.1 192.0.2.1 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 03:00:01 2025 UTC |
sorry, missing one line in the test script: Here's the correct one: ------------------- $servers = [ '192.0.2.1', '192.0.2.2', '192.0.2.3' ]; foreach ($servers as $server) { $resolveparam = [ 'example.com:443:'.$server ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 0); curl_setopt($ch, CURLOPT_RESOLVE, $resolveparam); curl_setopt($ch, CURLOPT_URL, "https://example.com/some/path"); curl_setopt($ch, CURLOPT_VERBOSE, 1); $result = curl_exec($ch); $info = curl_getinfo($ch); echo $info['primary_ip']."\n"; curl_close($ch); }