2011-07-15 80 views
0

我在我的服務器上運行以下代碼,該腳本在瀏覽器中正常工作,但是當我在cronjob中或通過shell以root用戶身份登錄時嘗試它得到完全不同的輸出。Cron作業有不同的輸出,然後在瀏覽器中運行腳本

輸出通過瀏覽器:

  • 日誌中,並獲取網頁,我想

通過shell/cron的輸出:

  • 似乎沒有登錄,但得到該頁面的內容將我重定向回登錄頁面。

我使用的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 


?> 
+0

你是否以root身份運行此腳本(來自cron)? – genesis

+0

「通過瀏覽器」是什麼意思?我認爲這是一個腳本,並通過瀏覽器將通過網址訪問它?還有什麼是cron作業命令?你有什麼特權? – Steven

+0

我以root用戶身份登錄並運行此命令.../usr/bin/php /var/www/vhosts/domain.co.uk/httpdocs/store/curl_test.php如果我轉到http:// www。 domain.com/store/curl_test.php它的工作原理 – Stu

回答

0

首先檢查命令的輸出,如果你執行它作爲你的網絡用戶(通常是www數據)

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」。

+0

我發現了PHP safe_mode在/etc/php.ini中設置爲On的原因,並且Apache配置文件中的safe_mode設置爲Off,現在它全部正常工作!乾杯 – Stu

相關問題