2011-04-28 60 views
0
use WWW::Curl::Easy; 

$curl->setopt(CURLOPT_HEADER,1);  
$curl->setopt(CURLOPT_RETURNTRANSFER,1);  
$curl->setopt(CURLOPT_URL,"http://foo.com/login.php"); 
$curl->setopt(CURLOPT_POSTFIELDS,"user=usertest&pass=passwdtest"); 
$curl->perform(); 

它將打印出這樣的結果。 如何從執行函數獲取輸出到變量中?如何將WWW的輸出:Curl :: Easy轉換爲Perl中的變量

HTTP/1.1 302實測值緩存控制: 無緩存,必重新驗證到期日: 星期六,01月11日200 5時00分○○秒GMT 地點:cookiecheck = 1 內容類型:文本/ html日期:星期四,28 Apr 2011 09:15:57 GMT服務器: xxxx/0.1內容長度:0 連接:保持活動設置Cookie: auth = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; expires =星期六,27-Apr-2013 09:15:57 GMT; path = /;域= .foo.com中

謝謝

回答

0

你到底想幹什麼?可能是LWP::Simple是你需要的...

2

我同意PacoRG,你很可能應該從LWP::空間使用模塊進行調查。既然你有更具體的需求,我會推薦LWP::UserAgent

這就是說,如果你真的需要得到一些正在打印的東西而不是存儲在一個變量中,我們可以用更深的Perl魔法玩一些遊戲。

# setopt method calls here 

## the variable you want to store your data in 
my $variable; 

{ 
    ## open a "filehandle" to that variable 
    open my $output, '>', \$variable; 

    ## then redirect STDOUT (where stuff goes when it is printed) to the filehandle $output 
    local *STDOUT = $output; 

    ## when you do the perform action, the results should be stored in your variable 
    $curl->perform(); 
} 

## since you redirected with a 'local' command, STDOUT is restored outside the block 
## since $output was opened lexically (with my), its filehandle is closed when the block ends 

# do stuff with $variable here 

也許WWW::Curl::Easy有這樣做的更好的辦法,因爲我不知道我爲你提供了一個黑客工具,將你所需要的是模塊的命令。

相關問題