file_get_html如何启用cookie

时间:2014-07-17 12:53:39

标签: php cookies

$url ="www.site.com"
$request = array('http' => array('user_agent' => 'UserAgent/1.0'));                 
$context = stream_context_create($request);     
$html = file_get_html($url, false, $context);   

$request如何在网站$url中保存Cookie?

3 个答案:

答案 0 :(得分:0)

很抱歉琐碎的答案,但你不能。

答案 1 :(得分:0)

你不能 - 因为cookie处理使用卷曲,否则你有责任从请求中保存和重复使用cookie。

在curl中,您可以使用选项COOKIEJARCOOKIEFILE

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: UserAgent/1.0'));

$response = curl_exec($ch);
curl_close($ch);

答案 2 :(得分:0)

参考:https://www.php.net/manual/en/function.file-get-contents.php

并做了一些修改以使用 file_get_html

<?php
$url ="www.site.com";
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n".
              "Cookie: foo=bar\r\n")`enter code here`
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_html($url, false, $context);
?>