Cron作业具有不同的输出,然后在浏览器中运行脚本

时间:2021-11-06 01:09:19

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.

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

output through the browser:

通过浏览器输出:

  • logs in and gets the page i want
  • 登录并获取我想要的页面

output through shell / cron:

通过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

我正在使用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


?>

1 个解决方案

#1


0  

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

如果您将其作为Web用户(通常是www-data)执行,请首先检查命令的输出

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".

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

#1


0  

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

如果您将其作为Web用户(通常是www-data)执行,请首先检查命令的输出

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".

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