I want to login to OpenCart CMS with curl automatically and add things to it automatic too
but I can't login to it.I searched and found some result but couldn't help me and I don't 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 of linking to
to admin of my website then I use CURLOPT_POST
request and CURLOPT_POSTFIELDS
and then execute the $ch
but now I don't have any result on cookie.txt
and didn't
login to website's admin.
I looked at http://demo.opencart.com/admin/ and saw that the action url in the form is not only /admin. Try this one:
<?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 didnt't test if the action url depends on a specific version, so just inspect the form with your browser developer tools
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With