PHP卷毛和曲奇饼 [英] PHP Curl And Cookies

查看:125
本文介绍了PHP卷毛和曲奇饼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP Curl和Cookie验证方面有一些问题。



我有一个文件 Connector.php ,用于验证另一台服务器上的用户



问题是,我想使用curl验证成千上万的用户,但它一次只为一个用户验证和保存COOKIE。 p>

connector.php的代码如下:

 <?php 
if(!count($ _ REQUEST)){
die(No Access!
}


//服务的核心URL
define('ServiceCore','http://example.com/core/');


//应该调用哪个内部服务
$ path = $ _GET ['service'];


//服务要查询
$ url = ServiceCore。$ path;

//打开Curl会话
$ session = curl_init($ url);

//如果是GET,将GET数据放在正文中
if($ _GET ['service']){
//迭代GET Vars
$ postvars ='';
foreach($ _ GET as $ key => $ val){
if($ key!='service'){
$ postvars。=$ key = $ val&
}
}
curl_setopt($ session,CURLOPT_POST,true);
curl_setopt($ session,CURLOPT_POSTFIELDS,$ postvars);
}


//创建并保存Cookie
$ tmpfname = dirname(__ FILE __)。'/ cookie.txt';
curl_setopt($ session,CURLOPT_COOKIEJAR,$ tmpfname);
curl_setopt($ session,CURLOPT_COOKIEFILE,$ tmpfname);

curl_setopt($ session,CURLOPT_HEADER,false);
curl_setopt($ session,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ session,CURLOPT_FOLLOWLOCATION,true);

// EXECUTE
$ json = curl_exec($ session);
echo $ json;
curl_close($ session);
?>

以下是认证过程:


  1. 用户输入用户名和密码: Connector.php?service = logon& user_name = user32& user_pass = 123

  2. Connector.php?service = logosessionInfo 会根据之前使用登录服务保存的Cookie返回有关用户的信息。

解决方案

您可以指定cookie文件与卷曲选择。您可以为每个用户使用唯一的文件。

  curl_setopt($ curl_handle,CURLOPT_COOKIESESSION,true); 
curl_setopt($ curl_handle,CURLOPT_COOKIEJAR,uniquefilename);
curl_setopt($ curl_handle,CURLOPT_COOKIEFILE,uniquefilename);

处理它的最好方法是将请求逻辑粘贴到curl函数中,唯一文件名作为参数。

 函数fetch($ url,$ z = null){
$ ch = curl_init();

$ useragent = isset($ z ['useragent'])? $ z ['useragent']:'Mozilla / 5.0(Windows NT 6.1; WOW64; rv:10.0.2)Gecko / 20100101 Firefox / 10.0.2';

curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_AUTOREFERER,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ ch,CURLOPT_POST,isset($ z ['post']));

if(isset($ z ['post']))curl_setopt($ ch,CURLOPT_POSTFIELDS,$ z ['post']);
if(isset($ z ['refer']))curl_setopt($ ch,CURLOPT_REFERER,$ z ['refer']);

curl_setopt($ ch,CURLOPT_USERAGent,$ useragent);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,(isset($ z ['timeout'])?$ z ['timeout']:5));
curl_setopt($ ch,CURLOPT_COOKIEJAR,$ z ['cookiefile']);
curl_setopt($ ch,CURLOPT_COOKIEFILE,$ z ['cookiefile']);

$ result = curl_exec($ ch);
curl_close($ ch);
return $ result;
}

我使用这个快速抓取。它需要url和一个选项数组。


I have some problem with PHP Curl and Cookies Authentication.

I have a file Connector.php which authenticates users on another server and returns cookie with current user.

The Problem is that I want to authenticate thousands of users with curl but it authenticates and saves COOKIES only for one user at a time.

the code for connector.php is this:

    <?php
    if(!count($_REQUEST)) {
        die("No Access!");
    }


    //Core Url For Services
    define ('ServiceCore', 'http://example.com/core/');


    //Which Internal Service Should Be Called
    $path = $_GET['service'];


    //Service To Be Queried
    $url = ServiceCore.$path;

    //Open the Curl session
    $session = curl_init($url);

    // If it's a GET, put the GET data in the body
    if ($_GET['service']) {
        //Iterate Over GET Vars
        $postvars = '';
        foreach($_GET as $key=>$val) {
            if($key!='service') {
                $postvars.="$key=$val&";
            }
        }
        curl_setopt ($session, CURLOPT_POST, true);
        curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
    }


    //Create And Save Cookies
    $tmpfname = dirname(__FILE__).'/cookie.txt';
    curl_setopt($session, CURLOPT_COOKIEJAR, $tmpfname);
    curl_setopt($session, CURLOPT_COOKIEFILE, $tmpfname);

    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);

    // EXECUTE
    $json = curl_exec($session);
        echo $json;
    curl_close($session);
?>

Here is the process of authentication:

  1. User enters username and password: Connector.php?service=logon&user_name=user32&user_pass=123
  2. Connector.php?service=logosessionInfo this returns info about user based on cookies saved earlier with logon service.

The problem is that this code saves Cookie in one file for one user and cant handle multiple user authentication.

解决方案

You can specify the cookie file with a curl opt. You could use a unique file for each user.

curl_setopt( $curl_handle, CURLOPT_COOKIESESSION, true );
curl_setopt( $curl_handle, CURLOPT_COOKIEJAR, uniquefilename );
curl_setopt( $curl_handle, CURLOPT_COOKIEFILE, uniquefilename );

The best way to handle it would be to stick your request logic into a curl function and just pass the unique file name in as a parameter.

    function fetch( $url, $z=null ) {
            $ch =  curl_init();

            $useragent = isset($z['useragent']) ? $z['useragent'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';

            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
            curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
            curl_setopt( $ch, CURLOPT_POST, isset($z['post']) );

            if( isset($z['post']) )         curl_setopt( $ch, CURLOPT_POSTFIELDS, $z['post'] );
            if( isset($z['refer']) )        curl_setopt( $ch, CURLOPT_REFERER, $z['refer'] );

            curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
            curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, ( isset($z['timeout']) ? $z['timeout'] : 5 ) );
            curl_setopt( $ch, CURLOPT_COOKIEJAR,  $z['cookiefile'] );
            curl_setopt( $ch, CURLOPT_COOKIEFILE, $z['cookiefile'] );

            $result = curl_exec( $ch );
            curl_close( $ch );
            return $result;
    }

I use this for quick grabs. It takes the url and an array of options.

这篇关于PHP卷毛和曲奇饼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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