PHP Curl登录和获取数据 [英] PHP Curl Login and fetch data

查看:186
本文介绍了PHP Curl登录和获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里建立一个可以登入Comcast的PHP应用程式。

I'm trying to make an php app that that logs on Comcast on here

https://login.comcast.net/login

并下载文件

http://xfinity.comcast.net/

这是我到目前为止:

<?php
$username="username"; 
$password="password"; 
$url="https://login.comcast.net/login"; 
$cookie="cookie.txt"; 

$postdata = "user=".$username."&passwd=".$password."&rm=2&deviceAuthn=false&forceAuthn=true&s=ccentral-cima&r=comcast.net&continue=http://xfinity.comcast.net/"; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
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, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  
curl_close($ch);
file_put_contents("page.txt",$ch);

echo $cookie;
?>

这个脚本做的只是登录并显示给我

What this script does is just login, and show me

https://customer.comcast.com/Public/Home.aspx

当我登录,然后当我按电子邮件,例如它只是对待我,因为我从来没有登录,请求我的用户名和密码。我想要的是通过保存 http://xfinity.comcast.net/ (与

as i was logged in, and then when i press Email for example it simply treats me like i have never logged in, requesting me username and password. All i want is to fetch the number of emails by saving http://xfinity.comcast.net/ (with a cookie) to page.txt

这可能吗?

推荐答案

p>首先,如果你把卷曲线放入一个函数,这将是方便。

First of all it would be handy if you put the curl lines into a function.

现在的步骤应该是相当容易。

Now the steps should be rather easy.

EDIT

确保cookie.txt与您的php文件在同一目录中。
并确保它是可写的。 (chmod 777)
检查:

EDIT
Make sure cookie.txt is in the same directory as your php file. and make sure it is writable. ( chmod 777 ) to check :

if (is_writable('cookie.txt')) {
    echo 'The cookie is writable';
} else {
    echo 'The cookie is not writable';
    ## start by making it writable :
   if (! chmod ( 'cookie.txt', 0777 ))
        die ( 'chmod() failed on file cookie.txt' );
}




  1. 使用curl登录和保存登录cookie数据。

  2. 执行另一个卷曲启用Cookie并检索网页。

像这样:

//@param string $url : url of page/file
    //@param bool $binary : binary file.
    //@param string $post : post data in format : formvar1=VAR1&formvar2=VAR2
    //@param string $cookie : cookie file.

function curl($url ,$binary=false,$post=false,$cookie =false ){

$ch = curl_init();

        curl_setopt ($ch, CURLOPT_URL, $url );
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);



        if($cookie){

            $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
            curl_setopt($ch, CURLOPT_USERAGENT, $agent);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);

        }


        if($binary)
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

        if($post){
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        }

     return  curl_exec ($ch);
}

然后登录:

$username="username"; 
$password="password"; 
$url="https://login.comcast.net/login"; 
$cookie="cookie.txt"; 

$postdata = "user=".$username."&passwd=".$password."&rm=2&deviceAuthn=false&forceAuthn=true&s=ccentral-cima&r=comcast.net&continue=http://xfinity.comcast.net/"; 

// function
$ch = curl($url,false,$postdata,$cookie);

登录?检查cookie.txt的内容。

Logged in ? check cookie.txt for content.

步骤2,获取第二页。

$url ='http://xfinity.comcast.net/'
$cookie="cookie.txt"; 

$ch =curl($url,false,false,$cookie);

echo $ch;

这样的话...让我知道它是否有效。

Something like that... Let me know if it works.

这篇关于PHP Curl登录和获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆