多个cURL操作,同一个会话 [英] Multiple cURL actions, same session

查看:275
本文介绍了多个cURL操作,同一个会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我知道curl_multi_init,但它不完全做我需要的,根据我的知识。

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

在一个登录后下载文件,所以

Im downloading files behind a login, so

a)需要登录
b)下载图片

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

$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);

任何帮助,非常感激。

推荐答案

我想出来,只是添加会话ID到cURL。但是一个快速和肮脏的方式是

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 --headerCookie:PHPSESSID ='。$ session_id。''。 $ image_url;

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

shell_exec($ download_str);

shell_exec($download_str);

这篇关于多个cURL操作,同一个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆