【发布时间】:2015-04-04 00:00:19
【问题描述】:
我正在尝试使用 PHP 的 cURL 从我的 Reddit 帐户获取提要。因此,我需要保留我的 cookie,目前我正在尝试使用 Netscape 格式的 cookies.txt 文件来执行此操作,但这些并没有应用于页面。我想我一定是在做一些愚蠢的事情。
最终结果我希望在显示之前让页面自动登录到 Reddit,但我希望在跳到更复杂的事情之前先学习一下。
<?php
//format cookies.txt file by adding semi colons to the end of new lines if they aren't already there and removing colons added to #'s
$cookies = file_get_contents('.\cookies.txt');
$cookies = str_replace("\n",";\n",$cookies);
$cookies = str_replace("#;","#",$cookies);
$cookies = str_replace(";;",";",$cookies);
file_put_contents("./cookies.txt", $cookies);
//set user agent and url + initiate
$user_agent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
$url = 'http://www.reddit.com';
$c = curl_init($url);
//My options
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>".\cookies.txt", //set cookie file
CURLOPT_COOKIEJAR =>".\cookies.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
curl_setopt_array( $c, $options );
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
//get html
echo $html;
?>
【问题讨论】: