Curl PHP发布安全登录

时间:2017-01-14 19:38:48

标签: php curl web-crawler

我需要检索此私人销售商店的广告系列/销售列表:www.showroomprive.com

我需要先使用这些凭据登录: 登录:stackoverflow37@gmail.com 密码:stackoverflow37

这是我的代码:

    <?

//First query is to retrieve __VIEWSTATE , __VIEWSTATEGENERATOR , ... which are necessary for login post


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://www.showroomprive.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
    curl_setopt($ch, CURLOPT_REFERER, 'https://www.amazon.fr/');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    $page = curl_exec($ch);


//Retrieving __VIEWSTATE , __VIEWSTATEGENERATOR , ... out of the html
    $page = preg_replace( "/\r|\n/", "", $page );
    preg_match_all('/.+?__VIEWSTATE" value="(.+?)".+?__VIEWSTATEGENERATOR" value="(.+?)".+?__EVENTVALIDATION" value="(.+?)"/', $page, $result);

    $__VIEWSTATE = $result[1][0];
    $__VIEWSTATEGENERATOR = $result[2][0];
    $__EVENTVALIDATION = $result[3][0];

    //preparing the login post query

    $header=array(
      'Connection: keep-alive',
      'Content-Length: 4932',
      'Cache-Control: max-age=0',
      'Origin: https://www.showroomprive.com',
      'Upgrade-Insecure-Requests: 1',
      'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
      'Content-Type: application/x-www-form-urlencoded',
      'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
      'Referer: https://www.showroomprive.com/Default.aspx?from=deconnect&p=accueil.aspx',
      'Accept-Encoding: gzip, deflate, br',
      'Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,de;q=0.2,it;q=0.2,ru;q=0.2',


    );



    $post_fields = array(
        '__EVENTTARGET' => "Login$LienLogin",
        '__EVENTARGUMENT' => "",
        'Login$tbLogin' => "stackoverflow37@gmail.com",
        'Registration$tb_prenom'=> "",
        'Registration$tb_nom'=> "",
        'Registration$tb_email'=> "",
        'Registration$tb_password'=> "",
        'Registration$tb_parrain'=> "",
        'Login$tbPass' => "stackoverflow37",
        'BottomSEO$DDLCountry' => "FR",
        '__VIEWSTATE' => $__VIEWSTATE,
        '__VIEWSTATEGENERATOR' => $__VIEWSTATEGENERATOR,
        '__EVENTVALIDATION' => $__EVENTVALIDATION
         );

    $fields_string ="";
    foreach($post_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    sleep(2);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://www.showroomprive.com/Default.aspx?from=deconnect&p=accueil.aspx");
    curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    curl_setopt($ch,CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: __gads=ID=3d4d19bc3863cdac:T=1483799974:S=ALNI_MbJn82hX9AxlSFu5-BXXP9YZVSXkg; _tlp=1156:6742245; srpFp=47; LiwioReferrer=http%3A//www.showroomprive.com/Accueil.aspx%3Ffbc%3D1; abt=1; _gat=1; _gat_analyticsTracker=1; _tli=477999116374116208; _tlc=:1484412282:www.showroomprive.com%2FDefault.aspx%3Fmes%3D%26direction%3D%252faccueil.aspx:showroomprive.com; _gat_audienceTracker=1; cdx_ses_1_11544=2637477935&1599dddfdd7&1599dddfdd7&; SRP=; SRPCRM=; SRPInscription=%3fmes%3d%26direction%3d%252faccueil.aspx; ABTastySession=LiwioHashMRASN%3Anull%5E%7C%5ELiwioUTMC%3A1%5E%7C%5ELiwioReferrerSent%3A1; ABTasty=LPT159050%3A-2.1483800004%5E%7C%5EActionTrackingEvents%3AClic%2520Ajouter%2520au%2520panier%2CAjout%2520Panier%2CAJOUT%2520PANIER%2CClic%2520sur%2520Ajout%2520Panier%5E%7C%5ELPT159868%3A221109.1483800257%5E%7C%5ELPT154065%3A-2.1483800004%5E%7C%5ELPT161532%3A225711.1483800004%5E%7C%5ELPT149830%3A-2.1483799968%5E%7C%5EABTastyUTMB%3A1%5E%7C%5ELiwioTracking%3A17010715390683616*159868.221109*161532.225711%5E%7C%5EsegmentationTracking%3A17010715390683616%5E%7C%5ELiwioUTMA%3A0.5.1483799946162.1484409283679.1484412281027.3*161532.5*159868.5; _ga=GA1.2.369897606.1483799946; _tlv=5.1483799947.1484410366.1484412435.54.6.3; _tls=*.763002,763004..477999116374116208; _lm_id=45I54MGV5FWBNW55; tCdebugLib=1"));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
    curl_setopt($ch, CURLOPT_REFERER, 'https://www.showroomprive.com/');
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    $page = curl_exec($ch);
    echo $page;

    ?>

问题在于,尽管标题和帖子与我的Chrome浏览器看起来完全一样,但我仍然将网站检测为卷曲查询。

我是否需要添加任何内容才能将curl查询视为浏览器?

1 个答案:

答案 0 :(得分:1)

您缺少参数的urlencoding。很高兴http_build_query为你做到了。

用该行替换自定义函数/ foreach部分

$fields_string = http_build_query($post_fields);

通知是由

引起的
 '__EVENTTARGET' => "Login$LienLogin",

由于" PHP正在寻找未定义的变量$LienLogin。您可以使用单引号(')。另请查看Guzzle,它是发出HTTP请求的绝佳客户端。

相关问题