1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-10 00:37:02 +02:00

Update in WireHttp to correct issue where curl POST method sometimes did not work due to 'expect' header that CURL adds to some requests, plus an extra boundary CURL adds to content-type

This commit is contained in:
Ryan Cramer
2022-07-08 09:43:14 -04:00
parent d4e2a39940
commit 6a9dfe654d
2 changed files with 20 additions and 4 deletions

View File

@@ -976,7 +976,7 @@ class WireDatabasePDO extends Wire implements WireDatabase {
*
* #pw-group-custom
*
* @param string $sql Query (string) to log, boolean true to reset/start query logging, boolean false to stop query logging
* @param string|bool $sql Query (string) to log, boolean true to reset/start query logging, boolean false to stop query logging
* @param string $note Any additional debugging notes about the query
* @return array|bool|int Returns query log array, boolean true on success, boolean false if not
*

View File

@@ -666,7 +666,6 @@ class WireHttp extends Wire {
$timeout = isset($options['timeout']) ? (float) $options['timeout'] : $this->getTimeout();
$postMethods = array('POST', 'PUT', 'DELETE', 'PATCH'); // methods for CURLOPT_POSTFIELDS
$isPost = in_array($method, $postMethods);
$contentType = isset($this->headers['content-type']) ? $this->headers['content-type'] : '';
$proxy = '';
if(!empty($options['proxy'])) {
@@ -695,11 +694,20 @@ class WireHttp extends Wire {
// not reachable: version blocked before sendCURL call
}
if($method === 'POST' && empty($this->headers['expect'])) {
// The 'expect' header that CURL uses waits for server to respond that the POST is okay,
// but many servers don't implement this, or ignore it, so we disable it here.
$this->headers['expect'] = '';
}
if(count($this->headers)) {
if($isPost && !empty($this->data) && $contentType === self::defaultPostContentType) {
/* kept for temporary reference:
if($isPost && !empty($this->data) && $this->>headers['content-type'] === self::defaultPostContentType) {
// CURL does not work w/default POST content-type when sending POST variables array
// if setting array (rather than query string) for CURLOPT_POSTFIELDS
$this->headers['content-type'] = 'multipart/form-data; charset=utf-8';
}
*/
$headers = array();
foreach($this->headers as $name => $value) {
$headers[] = "$name: $value";
@@ -722,7 +730,9 @@ class WireHttp extends Wire {
if(!empty($this->data)) {
if($isPost) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
// setting data as associative array adds a boundary to the content-type header that we dont
// want so we set value as query string from http_build_query rather than associative array
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->data));
} else {
$content = http_build_query($this->data);
if(strlen($content)) $url .= (strpos($url, '?') === false ? '?' : '&') . $content;
@@ -773,6 +783,12 @@ class WireHttp extends Wire {
curl_setopt($curl, $opt, $optVal);
}
}
// Enables it to work on URLs that set cookies then redirect
// such as: https://galesupport.com/novelGeo/novelGeoLink.php?loc=nysl_ca_sar&db=AONE
// $tempDir = $this->wire()->files->tempDir();
// $this->cookiePath = $tempDir->get();
// curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookiePath);
$result = curl_exec($curl);