dongmin1166 2015-10-04 12:47
浏览 22

将cookie与cURL PHP一起使用

I need to get the source code of a page that is only accessible when I am logged in. However, when I use the following code, it doesn't recognize that I'm logged in, and asks me to re-login.

$opts = array('http' => array('header'=> 'Cookie: ' . $COOKIEFILE."
"));
$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com/', false, $context);
echo $file;

I have researched and found examples similar to mine, but they don't help me. When I use the above code it doesn't recognize my cookies.

Full code:

$USERNAME = 'myEmail';
$PASSWORD = 'myPassword';
$COOKIEFILE = tempnam ("/tmp", "CURLCOOKIE");

$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

curl_setopt($ch, CURLOPT_URL, 
  'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
$data = curl_exec($ch);

$formFields = getFormFields($data);

$formFields['Email']  = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);

$post_string = '';
foreach($formFields as $key => $value) {
    $post_string .= $key . '=' . urlencode($value) . '&';
}

$post_string = substr($post_string, 0, -1);

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($ch);
$ch = curl_init ("http:/example.com");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $COOKIEFILE); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
var_dump($output);
  • 写回答

1条回答 默认 最新

  • duandai7601 2015-10-04 13:03
    关注

    Allen, in situations like yours, when I'm having trouble login with curl, what I normally do is use Live Http Headers for firefox to log my requests and then use the request headers with curl CURLOPT_HTTPHEADER, the cookies will be included in the headers.

    Flow:

    Log in to the website using your browser, once logged, log any request with Live Http Headers for firefox, copy and paste the request to an array, something like :

    $myHeaders = array(
    "Host: stackoverflow.com",
    "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0",
    "Accept: application/json, text/javascript, */*; q=0.01",
    "Accept-Language: en-US,en;q=0.5",
    "Accept-Encoding: gzip, deflate",
    "DNT: 1",
    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
    "X-Requested-With: XMLHttpRequest",
    "Referer: http://stackoverflow.com/questions/32933636/use-cookie-with-curl-php",
    "Content-Length: 482",
    "Cookie: mycookie :)",
    "Connection: keep-alive",
    "Pragma: no-cache"
    );
    

    Then, use the headers with curl:

    curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeaders );
    

    Notes:
    1- No need for CURLOPT_COOKIEJAR or CURLOPT_COOKIEFILE
    2- This works with almost every site I've tried.
    3- Some cookies may expire after a while, others, like google, never expire.

    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应