Observe the code below and note the commented curl options in the array. This code runs "as is" and returns a valid expected XML response.
protected function curlResponse($params)
{
// Returns valid XML
$xml = $this->TokenXml($params);
$len = strlen($xml);
// Returns array of headers Content-Type, SOAPAction and Content-Length
$headers = $this->TokenHeaders($len);
$curlopts = [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HEADER => FALSE,
CURLOPT_URL => $params['tokenURL'],
//CURLOPT_POSTFIELDS, $xml,
//CURLOPT_HTTPHEADER, $headers
];
$ch = curl_init();
curl_setopt_array($ch, $curlopts);
// Now swap the commented lines above and comment out these two. Fails.
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Uncomment the two lines in the array and comment out both curl_setopt() lines. The code fails.
By fail I mean the in the first case the recipient at $params['tokenURL'] returns a valid XML response with a token and in the second responds with errors as described below. The documentation says curl_setopt_array() is a perfectly legitimate alternative to repetitive curl_setopt().
Errors:
If just CURLOPT_HTTPHEADER is in the array, receives "The server cannot service the request because the media type is unsupported."
If just CURLOPT_POSTFIELDS is in the options array, the request times out/dies.
If BOTH options are in the options array, it returns a full HTML "help/usage" page that tells me it is not receiving or not parsing the XML: "The following operations are supported ....."
(My "operation" is listed on this page)
Is there an obvious explanation or something stupid I am overlooking?
$ lsb_release -a
Description: Ubuntu 18.04.3 LTS
Release: 18.04
$ php -v
PHP 7.2.19-0ubuntu0.18.04.2 (cli) (built: Aug 12 2019 19:34:28) ( NTS )
$ curl --version
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
$ docker -v
Docker version 19.03.2, build 6a30dfc