Page 1 of 1

Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 4:29 am
by Jsf
Hello,
I need to Schedule admin module function, so trying to login with curl, but without success.

I want to set up cron job to:
http://www.example.com/admin/index.php? ... nctionname

But can't, because need to login... Maybe someone have a solution how to do this?

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 4:39 am
by billynoah
assuming it's a database related task, why not just write the cron script to execute the query directly to the mysql server?

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 4:43 am
by billynoah
not sure if this would be helpful but here's another thread where we discussed a cron job to auto update specials end date: http://forum.opencart.com/viewtopic.php?f=20&t=116126

all you really need to do is form your query and you should be able to do pretty much anything.

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 6:31 am
by Jsf
billynoah wrote:assuming it's a database related task, why not just write the cron script to execute the query directly to the mysql server?
Yes, but also need execute the function manually from admin and I need quite a few things already written in admin models files, so I do not want write function in admin and do the same similar job with queries directly to mysql...

I found only this solution: http://opencart.hostjars.com/blog/16 , but it is old and not safe enough :(

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 8:20 am
by billynoah
you could easily write some code in your module to execute the bash script and have it available in the admin as well.

also, sounds like the link you gave is compatible with 1.5.. might need a few tweaks but i'm sure you could get it working if you know your way around the framework.

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 1:02 pm
by nvedia
This can help
You can first login to admin and then invoke your actions
http://blog.andyhunt.info/2011/12/21/us ... a-website/

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 4:47 pm
by Jsf
nvedia wrote:This can help
You can first login to admin and then invoke your actions
http://blog.andyhunt.info/2011/12/21/us ... a-website/
I try this example, but also without success. I get blank page or echo just "1"... I think I need to add &token=xxx to URL, but how to get token value after login?

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 7:18 pm
by MarketInSG
You cannot use curl to get to your admin page. The admin page requires the session token and the user to be logined. The easiest way is to move it to your front end, and adding a secret key which only your system knows.

Re: Curl admin page to set up cron job

Posted: Sun Jan 26, 2014 9:45 pm
by nvedia
Another option is to use an automation script like php selenium webdriver to get to admin page and perform your actions

Re: Curl admin page to set up cron job

Posted: Sun Feb 16, 2014 3:43 pm
by Tania201
Try Webcron Service EasyCron.com, use the "Cookie Support" feature.

Re: Curl admin page to set up cron job

Posted: Wed Jul 02, 2014 5:07 pm
by olgri
MarketInSG wrote:You cannot use curl to get to your admin page. The admin page requires the session token and the user to be logined.
You CAN use curl to get to OC admin area. You just have to pass $_SESSION over with the curl call. I.e.:

Code: Select all

session_start();
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();

//create cURL connection
$curl_connection = curl_init('http://yourdomainname.com/admin/index.php?route=common/login');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_COOKIE, $strCookie );

//create array of data to be posted
$post_data['username'] = 'yourusername';
$post_data['password'] = 'yourpassword';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
//print_r(curl_getinfo($curl_connection));
//echo curl_errno($curl_connection) . '-' . curl_error($curl_connection); 

//close the connection
curl_close($curl_connection);
Source:
http://nz1.php.net/curl_setopt#87112
and
http://thehungrycoder.com/general/how-t ... kopen.html

Re: Curl admin page to set up cron job

Posted: Tue Sep 23, 2014 1:54 am
by Jsf
Thanks for help. Finally it works! Only the one thing I need to know $token to access next page.
Do not know if it the best way, bet maybe would helpfull for others...

Code: Select all

$url = curl_getinfo($curl_connection)['url'];
$token = strstr($url, 'token='); 
and then

Code: Select all

curl_setopt($curl_connection, CURLOPT_URL, 'http://www.yourdomain.com/admin/index.php?route=module/yourmodule/funkction&'.$token);

Re: Curl admin page to set up cron job

Posted: Sat Sep 27, 2014 5:28 pm
by MarketInSG
well, what's the use of a cron job if you need to know the token as it will always be changing.