Issue
I'm trying to send a CURL request and I'm having some interesting issues. Even though I'm seemingly setting the headers correctly, using curl_getinfo() after the execution shows that there's no headers. The request times out on my server, and when using the command line version of curl. On my desktop, I can send the same curl request and get a result. I'm assuming it's a firewall issue, since accessing the site through a browser gives a Bitdefender mismatched certificate warning. Any ideas?
public static function GetCurlHandler() {
// Initialize CURL handler
$curlHandler = curl_init();
curl_setopt( $curlHandler, CURLOPT_COOKIEJAR, PATH_TO_COOKIES_FILE );
curl_setopt( $curlHandler, CURLOPT_COOKIEFILE, PATH_TO_COOKIES_FILE );
curl_setopt( $curlHandler, CURLOPT_CAINFO, PATH_TO_CACERT_PEM );
curl_setopt( $curlHandler, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $curlHandler, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curlHandler, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curlHandler, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $curlHandler, CURLOPT_ENCODING, 'gzip, deflate' );
curl_setopt( $curlHandler, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); // default to older CURL version for supporting older programs
$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Upgrade: h2'; // tell the server to use https2 if available
curl_setopt( $curlHandler, CURLOPT_HTTPHEADER, $headers );
curl_setopt($curlHandler, CURLOPT_HEADER, TRUE);
curl_setopt( $curlHandler, CURLINFO_HEADER_OUT, true );
return $curlHandler;
}
static function GetCapabilities($urlBase = 'https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0',$recursionDepth=1)
{ $url = $urlBase.'?f=json';
Log::info("Getting capabilities for $url");
$curlHandler = self::GetCurlHandler();
curl_setopt($curlHandler, CURLOPT_URL, $url);
$curlResponse = curl_exec( $curlHandler );
$info = curl_getinfo( $curlHandler );
print_r( $info );
print_r( curl_error( $curlHandler ) );
var_dump( $curlResponse );
echo "\n";
curl_close( $curlHandler );
$jsonObject = json_decode($curlResponse);
if( empty( $jsonObject ) ) {
\log::error( 'empty response from server, retrying with other HTTP version...' );
$curlHandler = self::GetCurlHandler();
curl_setopt($curlHandler, CURLOPT_URL, $url);
curl_setopt($curlHandler, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
$curlResponse = curl_exec( $curlHandler );
print_r( curl_error( $curlHandler ) );
curl_close( $curlHandler );
$jsonObject = json_decode( $curlResponse );
if( empty( $jsonObject ) ) {
var_dump( $curlResponse );
throw new \Exception( 'empty response from server' ); // This is where I end up
}
Resulting Text:
‣ 00:00:00.049 [INFO] Getting capabilities for https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0?f=json
Array
(
[url] => https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0?f=json
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 21.220237
[namelookup_time] => 0.002064
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[redirect_url] =>
[primary_ip] =>
[certinfo] => Array
(
)
[primary_port] => 0
[local_ip] =>
[local_port] => 0
[http_version] => 0
[protocol] => 0
[ssl_verifyresult] => 0
[scheme] =>
[appconnect_time_us] => 0
[connect_time_us] => 0
[namelookup_time_us] => 2064
[pretransfer_time_us] => 0
[redirect_time_us] => 0
[starttransfer_time_us] => 0
[total_time_us] => 21220237
)
Failed to connect to madison.rexburg.org port 443: Timed outbool(false)
‣ 00:00:21.281 [ERROR] empty response from server, retrying with other HTTP version...
Failed to connect to madison.rexburg.org port 443: Timed outbool(false)
‣ 00:00:42.513 [ERROR] Exception: empty response from server
Command Line Equivalent:
curl -L "https://madison.rexburg.org/mrgis/rest/services/Downtown/BLDGFootprint/FeatureServer/0?f=json" -H "Accept-Language: en-US,en;q=0.5" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0" -H "Accept-Encoding: gzip, deflate" -H "Upgrade: h2" --output - --verbose --http1.1
Server Information:
- PHP 7.4.33
- Windows NT 10.0 build 17763 (Windows Server 2016) AMD64
- Running in a Google Cloud Services VM
- Compiled in Visual C++ 2017
- Apache 2.0 Handler
- Configure command: cscript /nologo /e:jscript configure.js "--enable-snapshot-build" "--enable-debug-pack" "--with-pdo-oci=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--without-analyzer" "--with-pgo"
- CURL version 8.0.1 (Windows) libcurl/8.0.1
Relevant Infrastructure Info: I'm just a junior dev, so I'm going to try my best to provide some network and general infrastructure information.
Default route:
{ "creationTimestamp": "2017-12-08T13:34:07.643-08:00", "description": "Default route to the Internet.", "destRange": "0.0.0.0/0", "id": "4955173435988163488", "kind": "compute#route", "name": "default-route-b06db13fc2a2a102", "network": "projects/our-server/global/networks/default", "nextHopGateway": "projects/our-server/global/gateways/default-internet-gateway", "priority": 1000, "selfLink": "projects/our-server/global/routes/default-route-b06db13fc2a2a102" }
VPC Firewall information: HTTP traffic off, HTTPS traffic on, nothing blocking egress from relevant port
Solution
Most likely the script can't connect to the intended target. The most likely reasons for this are:
the host cannot resolve the DNS name correctly
there is no connectivity out to the internet
Test these in isolation:
<?php
error_rporting(E_ALL);
$ip=gethostbyname('madison.rexburg.org');
print "$ip=gethostbyname('madison.rexburg.org')<br />";
flush();
$errcode=0;
$errmsg='notrun';
$conn=fsockopen("192.225.178.54", 443, $errcode, $errmsg, 10);
if (!$conn) {
print "error ($errcode): $errmsg";
} else {
print "connectivity is good";
}
?>
Answered By - symcbean Answer Checked By - Katrina (WPSolving Volunteer)