API Login Error - OpenCart 3.0.2.0
Posted: Mon Jul 24, 2017 11:26 am
function do_curl_request($url, $params=array()) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/apicookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/apicookie.txt');
$params_string = '';
if (is_array($params) && count($params)) {
foreach($params as $key=>$value) {
$params_string .= $key.'='.$value.'&';
}
rtrim($params_string, '&');
curl_setopt($ch,CURLOPT_POST, count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
}
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
$url = 'http://localhost/opencart/index.php?route=api/login';
$fields = array(
'username' => 'newton',
'key' => 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8howhYlcKW5fkBOcF12spVvKR0fVP0PIoOM6GUnFJLwsqEWxAo37mShU0lPfQU53EWDFfRoKibOqq5KFaFSCILPm7x4UsFBLUp0gMhgRT6Rc4IS3kiHMParqIEETmriMIF7xg5uJ1F2UK4PViKAzqiH8gNFCAWdnn8botZGjRUxsAEdUxLTttlmWXf',
);
$response = do_curl_request($url, $fields);
var_dump($response);
---------------------------
Here is my response
string(653) "Notice: Undefined index: api_token in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/session.php on line 8Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/opencart/system/framework.php:42) in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/startup.php on line 99Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/opencart/system/framework.php:42) in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/startup.php on line 157"
What is the issue ? I tried adding 'api_token=1234' to my url then the error goes but still not logging in
Php error log
[24-Jul-2017 03:20:59 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`key` = 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8' at line 1<br />Error No: 1064<br />SELECT * FROM `oc_api` WHERE `username` = 'newton' `key` = 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8howhYlcKW5fkBOcF12spVvKR0fVP0PIoOM6GUnFJLwsqEWxAo37mShU0lPfQU53EWDFfRoKibOqq5KFaFSCILPm7x4UsFBLUp0gMhgRT6Rc4IS3kiHMParqIEETmriMIF7xg5uJ1F2UK4PViKAzqiH8gNFCAWdnn8botZGjRUxsAEdUxLTttlmWXf' AND status = '1'' in /Applications/MAMP/htdocs/opencart/system/library/db/mysqli.php:40
Stack trace:
#0 /Applications/MAMP/htdocs/opencart/system/library/db.php(45): DB\MySQLi->query('SELECT * FROM `...')
#1 /Applications/MAMP/htdocs/opencart/catalog/model/account/api.php(4): DB->query('SELECT * FROM `...')
#2 [internal function]: ModelAccountApi->login('newton', 'GWHdoHsQ4I1jxQ2...')
#3 /Applicatio in /Applications/MAMP/htdocs/opencart/system/library/db/mysqli.php on line 40
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/apicookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/apicookie.txt');
$params_string = '';
if (is_array($params) && count($params)) {
foreach($params as $key=>$value) {
$params_string .= $key.'='.$value.'&';
}
rtrim($params_string, '&');
curl_setopt($ch,CURLOPT_POST, count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
}
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
$url = 'http://localhost/opencart/index.php?route=api/login';
$fields = array(
'username' => 'newton',
'key' => 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8howhYlcKW5fkBOcF12spVvKR0fVP0PIoOM6GUnFJLwsqEWxAo37mShU0lPfQU53EWDFfRoKibOqq5KFaFSCILPm7x4UsFBLUp0gMhgRT6Rc4IS3kiHMParqIEETmriMIF7xg5uJ1F2UK4PViKAzqiH8gNFCAWdnn8botZGjRUxsAEdUxLTttlmWXf',
);
$response = do_curl_request($url, $fields);
var_dump($response);
---------------------------
Here is my response
string(653) "Notice: Undefined index: api_token in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/session.php on line 8Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/opencart/system/framework.php:42) in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/startup.php on line 99Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/opencart/system/framework.php:42) in /Applications/MAMP/htdocs/opencart/catalog/controller/startup/startup.php on line 157"
What is the issue ? I tried adding 'api_token=1234' to my url then the error goes but still not logging in
Php error log
[24-Jul-2017 03:20:59 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`key` = 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8' at line 1<br />Error No: 1064<br />SELECT * FROM `oc_api` WHERE `username` = 'newton' `key` = 'GWHdoHsQ4I1jxQ2SUv6FYu626OZrtivQ8nN1tycm5ld4x6CLoFsk5wpRKv60Y5vTk1gbqg8howhYlcKW5fkBOcF12spVvKR0fVP0PIoOM6GUnFJLwsqEWxAo37mShU0lPfQU53EWDFfRoKibOqq5KFaFSCILPm7x4UsFBLUp0gMhgRT6Rc4IS3kiHMParqIEETmriMIF7xg5uJ1F2UK4PViKAzqiH8gNFCAWdnn8botZGjRUxsAEdUxLTttlmWXf' AND status = '1'' in /Applications/MAMP/htdocs/opencart/system/library/db/mysqli.php:40
Stack trace:
#0 /Applications/MAMP/htdocs/opencart/system/library/db.php(45): DB\MySQLi->query('SELECT * FROM `...')
#1 /Applications/MAMP/htdocs/opencart/catalog/model/account/api.php(4): DB->query('SELECT * FROM `...')
#2 [internal function]: ModelAccountApi->login('newton', 'GWHdoHsQ4I1jxQ2...')
#3 /Applicatio in /Applications/MAMP/htdocs/opencart/system/library/db/mysqli.php on line 40