Cron 作业的输出与在浏览器中运行脚本不同

发布于 2024-11-24 02:52:42 字数 1966 浏览 2 评论 0原文

我在我的服务器上运行以下代码,该脚本在浏览器中运行良好,但是当我在 cronjob 中尝试它或通过以 root 身份登录的 shell 时,我会得到完全不同的输出。

通过浏览器输出:

  • 登录并获取我想要的页面

通过 shell / cron 输出:

  • 似乎没有登录,但获取页面内容将我重定向回登录页面。

我正在使用红帽 Linux 5

<?php

$url = "http://www.domain.com/shopper_lookup.asp"; // the url where to send the request
$fields_send = 'shopper_username=USERNAME&shopper_password=PASSWORD&Validate=1';
$agent = "Opera/9.63 (Windows NT 5.1; U; en-GB) Presto/2.1.1";
$reffer = "http://www.domain.com/login.html";
$cookie_file_path = "/var/www/vhosts/domain.co.uk/httpdocs/store/cookie"; // Cookie File Path with CHMOD 777

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_send);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result_source = curl_exec($ch);

var_dump($ch);

echo $result_source;




$url = "http://www.domain.com/receipt.asp?ms=&order_id=ordernumber"; // the url where to send the request
$agent = "Opera/9.63 (Windows NT 5.1; U; en-GB) Presto/2.1.1";
$reffer = "http://www.domain.com/contents.asp?ms=";
$cookie_file_path = "/var/www/vhosts/domain.co.uk/httpdocs/store/cookie"; // Cookie File Path with CHMOD 777

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result_source = curl_exec($ch);


echo $result_source; // Print the result page


?>

I'm running the following code on my server, the script works fine in the browser but when i try it in a cronjob or through shell logged in as root i get a totally different output.

output through the browser:

  • logs in and gets the page i want

output through shell / cron:

  • doesn't seem to login, but gets the contents of the page redirecting me back to the login page.

i'm using red hat linux 5

<?php

$url = "http://www.domain.com/shopper_lookup.asp"; // the url where to send the request
$fields_send = 'shopper_username=USERNAME&shopper_password=PASSWORD&Validate=1';
$agent = "Opera/9.63 (Windows NT 5.1; U; en-GB) Presto/2.1.1";
$reffer = "http://www.domain.com/login.html";
$cookie_file_path = "/var/www/vhosts/domain.co.uk/httpdocs/store/cookie"; // Cookie File Path with CHMOD 777

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_send);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result_source = curl_exec($ch);

var_dump($ch);

echo $result_source;




$url = "http://www.domain.com/receipt.asp?ms=&order_id=ordernumber"; // the url where to send the request
$agent = "Opera/9.63 (Windows NT 5.1; U; en-GB) Presto/2.1.1";
$reffer = "http://www.domain.com/contents.asp?ms=";
$cookie_file_path = "/var/www/vhosts/domain.co.uk/httpdocs/store/cookie"; // Cookie File Path with CHMOD 777

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_GET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result_source = curl_exec($ch);


echo $result_source; // Print the result page


?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-12-01 02:52:42

首先检查命令的输出,如果您以 Web 用户(通常是 www-data)身份执行它,

su www-data /usr/bin/php /var/www/vhosts/domain.co.uk/httpdocs/store/curl_test.php

如果有效,那么这是一个权限错误,因此请确保您运行 cron 作业的用户可以写入文件“/ var/www/vhosts/domain.co.uk/httpdocs/store/cookie”。

Firstly check the output of the command if you execute it as your web user (typically www-data) with

su www-data /usr/bin/php /var/www/vhosts/domain.co.uk/httpdocs/store/curl_test.php

If that works then this is a permissions error so ensure the user you're running the cron job as can write to the file "/var/www/vhosts/domain.co.uk/httpdocs/store/cookie".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文