下載phantomjs 官網地址http://phantomjs.org/download.html
創建getCookie.js
var page = require('webpage').create()
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'
page.open('http://www.xxxxxx.com/', function(status) {
if(status === "success") {
setInterval(function() {
var bodytxt = page.evaluate(function() {
if(document.title != 'Just a moment...') {
return true
}
return false
});
if(bodytxt) {
var duid = '';
var cfc = ''
for(var i in page.cookies) {
if(page.cookies[i].name == '__cfduid') {
duid = page.cookies[i].value;
}else if (page.cookies[i].name == 'cf_clearance') {
cfc = page.cookies[i].value;
}
}
console.log("__cfduid="+duid+";cf_clearance="+cfc)
phantom.exit();
}
},1000)
}
});
創建php文件,確保exec函數能運行。
<?php
$dir = dirname(__FILE__);
function getCookie() {
global $dir;
$output = [];
$content = '';
exec($dir.'/phantomjs '.$dir.'/getCookie.js',$output);
if(!empty($output)) {
$content = implode("\n",$output);
}
return $content;
}
$cookie = getCookie();
$url = 'http://www.xxxxx.com/';
$content = get_url_content($url, $cookie);
echo $content;
function get_url_content($url, $cookie) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_COOKIE,$cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36');
$content = curl_exec($ch);
curl_close($ch);
return $content;
}