【发布时间】:2011-10-06 05:24:15
【问题描述】:
我在我的服务器上运行以下代码,该脚本在浏览器中运行良好,但是当我在 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
?>
【问题讨论】:
-
您是否以 root 身份运行此脚本(来自 cron)?
-
“通过浏览器”是什么意思?我假设这是一个脚本,并且通过浏览器将通过 url 访问它?还有什么是cron作业命令?你有什么特权?
-
我以 root 身份通过 SSH 登录并运行此命令... /usr/bin/php /var/www/vhosts/domain.co.uk/httpdocs/store/curl_test.php 如果我转到domain.com/store/curl_test.php 它有效
-
将 PHP 作为 shell 脚本运行,您没有通过浏览器使用的任何“网络”设施 - 因此 cookie、会话等将无法工作(这可能就是您得到的原因发送回登录屏幕?)
-
您声称此脚本曾经通过命令行在另一台服务器上运行,并且在从浏览器调用时可以运行。由于 PHP 通常为 web 服务器和命令行使用不同的 php.ini 配置,因此比较两个配置(如果存在)可能会产生正确的方向。