Post by dinpsnl » 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

Newbie

Posts

Joined
Sat Jul 22, 2017 11:46 am

Post by vimalmultimedia » Sat Aug 19, 2017 12:30 am

Hi.... I am facing the same issue. anyone has the solution?


Posts

Joined
Sat Aug 19, 2017 12:29 am

Post by boicute.14 » Fri Aug 25, 2017 11:55 am

Me too, im try more times, change method post to get ...
I think Opencart api its not complete, becase we had an SQL error and im found this.

Code: Select all

$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' `key` = '" . $this->db->escape($key) . "' AND status = '1'");
row 4 in file catalog/model/account/api.php and replace by

Code: Select all

$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' AND `key` = '" . $this->db->escape($key) . "' AND status = '1'");
my API login is work.

Newbie

Posts

Joined
Sat Oct 22, 2016 2:08 pm

Post by tdhungit » Wed Sep 06, 2017 1:39 pm

I found 2 error in OpenCart code
  1. In Model: catalog/model/account/api.php --> function login()

Code: Select all

public function login($username, $key) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' `key` = '" . $this->db->escape($key) . "' AND status = '1'");

		return $query->row;
	}
Fixed: change it as:

Code: Select all

public function login($username, $key) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "api` WHERE `username` = '" . $this->db->escape($username) . "' AND `key` = '" . $this->db->escape($key) . "' AND status = '1'");

		return $query->row;
	}
  • In Controller: catalog/controller/api/login.php
Line 30

Code: Select all

$session = new Session($config->get('session_engine'), $registry);
Fixed: change it as:

Code: Select all

$session = new Session($this->config->get('session_engine'), $this->registry);
It's work

Newbie

Posts

Joined
Wed Sep 06, 2017 1:33 pm

Post by Storeship » Wed Apr 25, 2018 5:59 pm

Thanks That worked, cant believe this untested code is default?

Newbie

Posts

Joined
Wed Sep 07, 2016 7:00 pm
Who is online

Users browsing this forum: No registered users and 16 guests