Login to Opencart admin with curl of PHP

I want to enter OpenCart CMS with a twist automatically and add automatic to it too, but I canโ€™t enter it. I searched and found some result, but could not help, and I do not get any result. This is my code:

<?php include_once "simple_html_dom.php"; $username = 'active'; $password = '123active'; $loginUrl = 'http://localhost:100/mywebsite/admin/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $loginUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password); curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec($ch); $html=new simple_html_dom(); $html->load($store); foreach($html->find("li#dashboard") as $dash){ echo $dash->innertext; } ?> 

In this code, first I initialize username and password and my URL to bind to the administrator of my site, then I use the CURLOPT_POST request and CURLOPT_POSTFIELDS
and then execute $ch but now I have no result on cookie.txt and there was no login on the admin site.

+5
source share
1 answer

I looked at http://demo.opencart.com/admin/ and saw that the action URL in the form is not only / admin. Try the following:

 <?php include_once "simple_html_dom.php"; $username = 'active'; $password = '123active'; //$loginUrl = 'http://localhost:100/mywebsite/admin/'; //new url $loginUrl = 'http://localhost:100/mywebsite/admin/index.php?route=common/login'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $loginUrl); curl_setopt($ch, CURLOPT_POST, true ); // follows a location header redirect curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password); curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec($ch); $html=new simple_html_dom(); $html->load($store); foreach($html->find("li#dashboard") as $dash){ echo $dash->innertext; } ?> 

I have not tested whether the action URL depends on a specific version, so just view the form using your browserโ€™s developer tools.

+1
source

All Articles