Opencart 2 admin api 在维护模式下调用
Opencart 2 admin api calls when in maintenance mode
当我将网站置于维护模式并尝试使用来自管理员的订单历史记录时,我得到:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of
the JSON data
如何在 curl 请求中传递身份验证数据,以便在以管理员身份登录时禁用维护模式?
public function api() {
$json = array();
// Store
if (isset($this->request->get['store_id'])) {
$store_id = $this->request->get['store_id'];
} else {
$store_id = 0;
}
$this->load->model('setting/store');
$store_info = $this->model_setting_store->getStore($store_id);
if ($store_info) {
$url = $store_info['ssl'];
} else {
$url = HTTPS_CATALOG;
}
if (isset($this->session->data['cookie']) && isset($this->request->get['api'])) {
// Include any URL perameters
$url_data = array();
foreach ($this->request->get as $key => $value) {
if ($key != 'route' && $key != 'token' && $key != 'store_id') {
$url_data[$key] = $value;
}
}
$curl = curl_init();
// Set SSL if required
if (substr($url, 0, 5) == 'https') {
curl_setopt($curl, CURLOPT_PORT, 443);
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=' . $this->request->get['api'] . ($url_data ? '&' . http_build_query($url_data) : ''));
if ($this->request->post) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->request->post));
}
curl_setopt($curl, CURLOPT_COOKIE, session_name() . '=' . $this->session->data['cookie'] . ';');
$json = curl_exec($curl);
curl_close($curl);
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput($json);
}
我在其他方法中注意到它们是对 api/login 的额外请求。有什么办法可以将两者合并
$api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));
if ($api_info) {
$curl = curl_init();
// Set SSL if required
if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
curl_setopt($curl, CURLOPT_PORT, 443);
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));
$json = curl_exec($curl);
if (!$json) {
$this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
} else {
$response = json_decode($json, true);
if (isset($response['cookie'])) {
$this->session->data['cookie'] = $response['cookie'];
}
curl_close($curl);
}
}
这是 OpenCart github 问题列表中多次提到的内容。此问题尚未修复,但 this commit 显示了您需要做什么才能使其正常工作
当我将网站置于维护模式并尝试使用来自管理员的订单历史记录时,我得到:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
如何在 curl 请求中传递身份验证数据,以便在以管理员身份登录时禁用维护模式?
public function api() {
$json = array();
// Store
if (isset($this->request->get['store_id'])) {
$store_id = $this->request->get['store_id'];
} else {
$store_id = 0;
}
$this->load->model('setting/store');
$store_info = $this->model_setting_store->getStore($store_id);
if ($store_info) {
$url = $store_info['ssl'];
} else {
$url = HTTPS_CATALOG;
}
if (isset($this->session->data['cookie']) && isset($this->request->get['api'])) {
// Include any URL perameters
$url_data = array();
foreach ($this->request->get as $key => $value) {
if ($key != 'route' && $key != 'token' && $key != 'store_id') {
$url_data[$key] = $value;
}
}
$curl = curl_init();
// Set SSL if required
if (substr($url, 0, 5) == 'https') {
curl_setopt($curl, CURLOPT_PORT, 443);
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url . 'index.php?route=' . $this->request->get['api'] . ($url_data ? '&' . http_build_query($url_data) : ''));
if ($this->request->post) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->request->post));
}
curl_setopt($curl, CURLOPT_COOKIE, session_name() . '=' . $this->session->data['cookie'] . ';');
$json = curl_exec($curl);
curl_close($curl);
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput($json);
}
我在其他方法中注意到它们是对 api/login 的额外请求。有什么办法可以将两者合并
$api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));
if ($api_info) {
$curl = curl_init();
// Set SSL if required
if (substr(HTTPS_CATALOG, 0, 5) == 'https') {
curl_setopt($curl, CURLOPT_PORT, 443);
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, $this->request->server['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, HTTPS_CATALOG . 'index.php?route=api/login');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($api_info));
$json = curl_exec($curl);
if (!$json) {
$this->error['warning'] = sprintf($this->language->get('error_curl'), curl_error($curl), curl_errno($curl));
} else {
$response = json_decode($json, true);
if (isset($response['cookie'])) {
$this->session->data['cookie'] = $response['cookie'];
}
curl_close($curl);
}
}
这是 OpenCart github 问题列表中多次提到的内容。此问题尚未修复,但 this commit 显示了您需要做什么才能使其正常工作