我有一个 WordPress 博客,设置为需要登录才能查看其中的任何内容。这是一个内部工具,但我们希望它可以从外部访问。这行得通,没问题。
但是有了这个设置,祝你访问 RSS 提要好运。
我认为使用 curl 登录,然后请求所需的页面并输出它不应该是火箭科学。此脚本将存在于内部网络服务器上,因此您可以在内部获取 RSS 提要(因此使用 Outlook 的用户,如所有者,可以在连接到网络时查看它)。
我看了一大堆关于如何做到这一点的教程和问题,但没有运气( here are a few of them )。他们似乎都没有完整的剧本。他们都做点点滴滴,我只是不能一起工作。
这是我目前所拥有的:
$username='dummyUser';
$password='dummyPassword';
$url='http://url.wordpress.org/';
$postdata = 'log='. $username .'&pwd='. $password .'&wp-submit=Log%20In&redirect_to='. $url .'wp-admin/&testcookie=1';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . 'wp-login.php');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, $url . 'wp-admin/');
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
$end = strpos($result, 'Content-Type');
$start = strpos($result, 'Set-Cookie');
$parts = split('Set-Cookie: ',substr($result, $start, $end-$start));
$cookies = array();
foreach ($parts as $co) {
$cd = split(';',$co);
if (!empty($cd[0]))
$cookies[] = $cd[0];
}
curl_setopt ($ch, CURLOPT_URL, $url . 'feed/');
curl_setopt ($ch, CURLOPT_COOKIE, implode(';',$cookies));
curl_setopt ($ch, CURLOPT_HEADER, 0);
$result = curl_exec ($ch);
echo $result;
我得到的最终结果只是显示了登录屏幕,所以我猜 cookie 有什么问题(我试着按照 this example )。任何能看到我所缺少的东西的人都将不胜感激。
最佳答案
尝试以下操作:
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
关于php - 使用 cURL 登录到特定的 WordPress 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918489/