Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
使用php/cURL登录站点并访问特定页面_Php_Asp.net_Curl_Login - Fatal编程技术网

使用php/cURL登录站点并访问特定页面

使用php/cURL登录站点并访问特定页面,php,asp.net,curl,login,Php,Asp.net,Curl,Login,我对php/cURL相当陌生,我正试图通过php登录Barnes and noble网站并访问热卖书部分,即我的php代码在运行时应该登录到我的帐户并显示Barnes and nobles网站的热卖书部分。我已经附上了代码和登录工程很好,但我不知道如何访问热卖书页之后,任何帮助将是伟大的 谢谢 <?php // options $EMAIL = 'xxxx'; $PASSWORD = 'yyyy'; $cookie_file_path = "/tmp

我对php/cURL相当陌生,我正试图通过php登录Barnes and noble网站并访问热卖书部分,即我的php代码在运行时应该登录到我的帐户并显示Barnes and nobles网站的热卖书部分。我已经附上了代码和登录工程很好,但我不知道如何访问热卖书页之后,任何帮助将是伟大的

谢谢

<?php

// options
$EMAIL            = 'xxxx';
$PASSWORD         = 'yyyy';
$cookie_file_path = "/tmp/cookies.txt";
$LOGINURL         = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; 
$agent            = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";


// begin script
$ch = curl_init(); 

// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";

// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);         
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);

// execute session to get cookies and required form inputs
$content = curl_exec($ch); 

// grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields['emailAddress'] = $EMAIL;
$fields['acctPassword'] = $PASSWORD;

// get x value that is used in the login url
$x = '';
if (preg_match('/op\.asp\?x=(\d+)/i', $content, $match)) {
$x = $match[1];
}

//$LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
$LOGINURL   = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";



// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($fields); 

// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL); 

// set post options
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); 

// perform login
$result = curl_exec($ch);  

print $result; 


function getFormFields($data)
{
if (preg_match('/(<form action="op.*?<\/form>)/is', $data, $matches)) {
    $inputs = getInputs($matches[1]);

    return $inputs;
} else {
    die('didnt find login form');
}
}

function getInputs($form)
{
$inputs = array();

$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

if ($elements > 0) {
    for($i = 0; $i < $elements; $i++) {
        $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

        if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
            $name  = $name[1];
            $value = '';

            if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                $value = $value[1];
            }

            $inputs[$name] = $value;
        }
    }
}


return $inputs;
}