Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用curl登录后获取动态生成的内容_Javascript_Php_Jquery_Curl_Dynamic - Fatal编程技术网

Javascript 使用curl登录后获取动态生成的内容

Javascript 使用curl登录后获取动态生成的内容,javascript,php,jquery,curl,dynamic,Javascript,Php,Jquery,Curl,Dynamic,我知道curl不执行javascript,它只捕获静态html,所以这就是为什么简单的curl对我不起作用的原因。 我对php不太了解,我是新手,但我的理解是,如果我不必首先登录来获取内容,我可以简单地使用file_get_contents,它将首先执行动态内容,然后获取html内容,反过来,它会给我所需的内容,但我首先必须登录,然后获取页面。 我尝试使用curl登录 $user = "myuser"; $pass = "mypassword"; //create cookie file $r

我知道curl不执行javascript,它只捕获静态html,所以这就是为什么简单的curl对我不起作用的原因。 我对php不太了解,我是新手,但我的理解是,如果我不必首先登录来获取内容,我可以简单地使用file_get_contents,它将首先执行动态内容,然后获取html内容,反过来,它会给我所需的内容,但我首先必须登录,然后获取页面。 我尝试使用curl登录

$user = "myuser";
$pass = "mypassword";

//create cookie file
$random = rand(0,9999999);
$cookie = $random."cookie.txt";
$fp = fopen("$cookie","w") or die("<BR><B>Unable to open cookie file $cookie_file_path for write!<BR>");
fclose($fp);

//do login using curl
$LOGINURL = "https://controlpanel.example.com/index.html";
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0";
$v2 = array( 'userName'=>$user, 'password'=>$pass);
$reffer = "https://www.google.com";
//this first call is to set the cookie
$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_URL,$LOGINURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
ob_start();      // Prevent output
curl_exec ($ch);
ob_end_clean();  // End preventing output
curl_close ($ch);
unset($ch);
//now that the cookie is set, do login
$ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$v2); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_URL,$LOGINURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_REFERER, $reffer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

$result = curl_exec($ch);

//now we are logged-in
//now grab the page you need

$profileurl = 'https://controlpanel.example.com/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$result = curl_exec ($ch);
作为一个noob,我从中了解到,内容是使用jquery动态加载的,curl不知道如何做到这一点

我试着把它放进去,而不是

$profileurl = 'https://controlpanel.example.com/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$result = curl_exec ($ch);

//replaced the above with this
$result = file_get_contents($profileurl);
但我从登录页面获取html,因为我认为它不再识别我登录了


那么我该如何解决这个问题呢?你能帮帮我吗?

我想我知道你在做什么

这里的关键是,大多数网站都使用cookie进行登录。在中,如果网站在您登录浏览器后设置了cookie,那么好消息是您可以解决此问题

代码中的问题是,PHP不会为您设置cookie

您需要两个步骤:

第一步。您需要在登录时获取cookie

下面是如何从登录页面返回cookie头

$ch = curl_init('https://controlpanel.example.com/index.html');

....

$result = curl_exec($ch);
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
echo $cookies;//See if you've successfully obtained the return cookie

第二步。您可以使用在步骤1中获得的cookie进行访问。(就像你已经在自己的代码中做的那样)

哈哈,太简单了,我没有想到。 对我来说很简单,我不需要打电话

但是

要得到我想要的div:)

幸运的是,我注意到firebug中的get函数:)

那么现在的化学需氧量是:

$user = "myuser";
$pass = "mypassword";

//create cookie file
$random = rand(0,9999999);
$cookie = $random."cookie.txt";
$fp = fopen("$cookie","w") or die("<BR><B>Unable to open cookie file $cookie for write!<BR>");
fclose($fp);

//do login using curl
$LOGINURL = "https://controlpanel.example.com/index.html";
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0";
$v2 = array( 'userName'=>$user, 'password'=>$pass);
$reffer = "https://www.google.com";
//this first call is to set the cookie

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_URL,$LOGINURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
ob_start();      // Prevent output
curl_exec ($ch);
ob_end_clean();  // End preventing output
curl_close ($ch);
unset($ch);

//now that the cookie is set, do login
$ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$v2); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_URL,$LOGINURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_REFERER, $reffer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

$result = curl_exec($ch);

//now we are logged-in
//now grab the page you need

$profileurl = 'https://controlpanel.example.com/async/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);

$result = curl_exec ($ch);
$user=“myuser”;
$pass=“mypassword”;
//创建cookie文件
$random=兰特(0999999);
$cookie=$random.“cookie.txt”;
$fp=fopen($cookie”,“w”)或die(
无法打开cookie文件$cookie进行写入!
); fclose($fp); //使用curl登录 $LOGINURL=”https://controlpanel.example.com/index.html"; $agent=“Mozilla/5.0(Windows NT 6.1;WOW64;rv:29.0)Gecko/20101 Firefox/29.0”; $v2=数组('userName'=>$user,'password'=>$pass); $reffer=”https://www.google.com"; //第一个调用是设置cookie $ch=curl_init(); curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie); curl_setopt($ch,CURLOPT_URL,$LOGINURL); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_USERAGENT,$agent); ob_start();//阻止输出 curl_exec($ch); ob_end_clean();//端阻输出 卷曲关闭($ch); 未结算($ch); //既然cookie已经设置好了,请登录 $ch=curl_init(); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_POSTFIELDS,$v2); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie); curl_setopt($ch,CURLOPT_URL,$LOGINURL); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); curl_setopt($ch,CURLOPT_REFERER,$reffer); curl_setopt($ch,CURLOPT_USERAGENT,$agent); $result=curl\u exec($ch); //现在我们已经登录了 //现在抓住你需要的页面 $profileurl='0https://controlpanel.example.com/async/information.html'; curl_setopt($ch,CURLOPT_URL,$profileurl); curl_setopt($ch,CURLOPT_POST,0); $result=curl\u exec($ch);
您理解错误,文件内容无法执行javascript。php中内置的任何东西都不会改变这一点。你的选择是1)手动解析返回的源代码,计算javascript端点并直接调用它们,或者2)安装一个无头浏览器,比如phantomjs,它执行JavaScripts。这个问题被标记为jquery,你能使用jquery吗?如果是这样,你可以考虑使用USER 57632的简单解决方案,我看到人们倾向于重定向到幻像,但是我如何使用它呢?我在看文档,但不知道如何使用它。我需要的是抓住那一页,我不在乎怎么做。我改变了最初的问题,以便更好地解释isI将尝试解决的问题,我现在没有时间,我会让你知道。但我不是已经在我的第一个卷曲请求中这样做了吗?这段代码不是用来设置cookie的吗?我改变了最初的问题,以便更好地解释问题是什么
$profileurl = 'https://controlpanel.example.com/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$result = curl_exec ($ch);

//replaced the above with this
$result = file_get_contents($profileurl);
$ch = curl_init('https://controlpanel.example.com/index.html');

....

$result = curl_exec($ch);
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
echo $cookies;//See if you've successfully obtained the return cookie
$user = "myuser";
$pass = "mypassword";

//create cookie file
$random = rand(0,9999999);
$cookie = $random."cookie.txt";
$fp = fopen("$cookie","w") or die("<BR><B>Unable to open cookie file $cookie for write!<BR>");
fclose($fp);

//do login using curl
$LOGINURL = "https://controlpanel.example.com/index.html";
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0";
$v2 = array( 'userName'=>$user, 'password'=>$pass);
$reffer = "https://www.google.com";
//this first call is to set the cookie

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_URL,$LOGINURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
ob_start();      // Prevent output
curl_exec ($ch);
ob_end_clean();  // End preventing output
curl_close ($ch);
unset($ch);

//now that the cookie is set, do login
$ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$v2); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_URL,$LOGINURL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_REFERER, $reffer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

$result = curl_exec($ch);

//now we are logged-in
//now grab the page you need

$profileurl = 'https://controlpanel.example.com/async/information.html';
curl_setopt($ch, CURLOPT_URL, $profileurl);
curl_setopt($ch, CURLOPT_POST, 0);

$result = curl_exec ($ch);