开发者

Multiple cURL actions, same session

First of all I'm aware of curl_multi_init but it doesn't exactly do what I need, to my knowledge.

Im d开发者_运维技巧ownloading files behind a login, so

a) Need to login b) Download image(s)

Here is what I currently have

$login_url = 'https://us.test.com/Member/Login';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$login_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'login='.$this->username.'&password='.$this->userpass.'&signin=1&login_referer=none&remember_me=1');
curl_setopt($ch, CURLOPT_FAILONERROR,1);  
curl_setopt($ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/test/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/test/cookie.txt');  
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, true);   
curl_exec($ch);

//Grab and Write the image
$image = file_get_contents($image_url); 
$f = fopen($_SERVER['DOCUMENT_ROOT'].'/test/uploads/'.$id.'.jpg', 'w');
fwrite($f, $image);
fclose($f);  


curl_close($ch);

Anything helps, much appreciated.


I figured it out, just added session ID into cURL. But a quick and dirty way is

$download_str = 'wget -O '. $save . $id . '.jpg --cookies=off --header "Cookie: PHPSESSID=' . $session_id . '" ' . $image_url;

shell_exec($download_str);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜