* Adjust http method for repo api.

This commit is contained in:
zhaoke
2023-08-04 09:22:05 +08:00
parent a27077fd4d
commit 2ec6e191fe
+45 -11
View File
@@ -3283,8 +3283,12 @@ EOF;
commonModel::$requestErrors = array();
$requestType = 'GET';
if(!is_array($headers)) $headers = (array)$headers;
$headers[] = "API-RemoteIP: " . helper::getRemoteIp();
$headers[] = 'API-RemoteIP: ' . helper::getRemoteIp(); /* Real IP of real user. */
$headers[] = 'API-LocalIP: ' . zget($_SERVER, 'SERVER_ADDR', ''); /* Server IP of self. */
if($dataType == 'json')
{
$headers[] = 'Content-Type: application/json;charset=utf-8';
@@ -3297,7 +3301,7 @@ EOF;
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_ENCODING,'');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
@@ -3312,14 +3316,37 @@ EOF;
if(is_object($data)) $data = (array) $data;
if($method == 'POST') curl_setopt($curl, CURLOPT_POST, true);
if(in_array($method, array('PATCH', 'PUT'))) curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
$requestType = $method;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if($options) curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$errors = curl_error($curl);
if($httpCode) $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
$errno = curl_errno($curl);
$errors = empty($errno) ? 0 : curl_error($curl);
$info = curl_getinfo($curl);
if($httpCode)
{
curl_setopt($curl, CURLOPT_HEADER, true); /* true to include the header in the output. */
$httpCode = $info['http_code'] ?? curl_getinfo($curl, CURLINFO_HTTP_CODE);
$headerSize = $info['header_size'] ?? curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headerString = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
/* Parse header. */
$header = explode('\n', $headerString);
$newHeader = array();
foreach($header as $item)
{
$field = explode(':', $item);
if(count($field) < 2) continue;
$headerKey = array_shift($field);
$newHeader[$headerKey] = implode('', $field);
}
}
curl_close($curl);
if($log or $app->config->debug)
@@ -3327,21 +3354,28 @@ EOF;
$logFile = $app->getLogRoot() . 'saas.'. date('Ymd') . '.log.php';
if(!file_exists($logFile)) file_put_contents($logFile, '<?php die(); ?' . '>');
$fh = @fopen($logFile, 'a');
$fh = fopen($logFile, 'a');
if($fh)
{
fwrite($fh, date('Ymd H:i:s') . ": " . $app->getURI() . "\n");
fwrite($fh, "url: " . $url . "\n");
if(!empty($data)) fwrite($fh, "data: " . print_r($data, true) . "\n");
fwrite($fh, "results:" . print_r($response, true) . "\n");
if(!empty($errors)) fwrite($fh, "errors: " . $errors . "\n");
fwrite($fh, date('Ymd H:i:s') . ': ' . $app->getURI() . "\n");
fwrite($fh, "{$requestType} url: " . $url . "\n");
if(!empty($data)) fwrite($fh, 'data: ' . print_r($data, true) . "\n");
fwrite($fh, 'results:' . print_r($response, true) . "\n");
if(!empty($errors))
{
fwrite($fh, 'errno: ' . $errno . "\n");
fwrite($fh, 'errors: ' . $errors . "\n");
fwrite($fh, 'info: ' . print_r($info, true) . "\n");
}
fclose($fh);
}
}
if($errors) commonModel::$requestErrors[] = $errors;
return $httpCode ? array($response, $httpCode) : $response;
return $httpCode ? array($response, $httpCode, 'body' => $body, 'header' => $newHeader, 'errno' => $errno, 'info' => $info, 'response' => $response) : $response;
}
/**