使用PHP cURL登录到远程站点

使用PHP cURL登录到远程站点,php,curl,Php,Curl,我不熟悉使用cURL,很难找到好的资源。 我试图做的是登录到一个远程站点,让curl完成登录表单,然后返回成功的消息 我的代码似乎不起作用,只是试图显示网站的主页 $username="mylogin@gmail.com"; $password="mypassword"; $url="http://www.myremotesite.com/index.php?page=login"; $cookie="cookie.txt"; $postdata = "email=".$use

我不熟悉使用cURL,很难找到好的资源。 我试图做的是登录到一个远程站点,让curl完成登录表单,然后返回成功的消息

我的代码似乎不起作用,只是试图显示网站的主页

    $username="mylogin@gmail.com"; 
$password="mypassword"; 
$url="http://www.myremotesite.com/index.php?page=login"; 
$cookie="cookie.txt"; 

$postdata = "email=".$username."&password=".$password; 

$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);

我做错了什么。工作完成后,我想重定向到另一个页面并从我的站点获取内容。

查看登录页面的来源。查找
表单
HTML标记。在该标记中有一些类似于
action=
的内容,将该值用作
$url
,而不是表单本身的url

此外,当您在那里时,请验证输入框的名称是否与您列出的名称相同

例如,基本登录表单类似于:

<form method='post' action='postlogin.php'>
    Email Address: <input type='text' name='email'>
    Password: <input type='password' name='password'>
</form>
验证您在
$postdata
中列出的值:

$postdata = "email=".$username."&password=".$password;

它应该可以正常工作。

我有同样的问题,我找到了这个答案

我只是稍微改变了一下(卷曲在最后一行结束)

我想这就是你想要的,对吗



还有一个有用的相关问题。关于如何在cUrl中保持会话的活动性:

我已经让它保持了一段时间,但后来又重新访问了它。因为这个问题经常被看。这就是我最终使用的对我有用的东西

define("DOC_ROOT","/path/to/html");
//username and password of account
$username = trim($values["email"]);
$password = trim($values["password"]);

//set the directory for the cookie using defined document root var
$path = DOC_ROOT."/ctemp";
//build a unique path with every request to store. the info per user with custom func. I used this function to build unique paths based on member ID, that was for my use case. It can be a regular dir.
//$path = build_unique_path($path); // this was for my use case

//login form action url
$url="https://www.example.com/login/action"; 
$postinfo = "email=".$username."&password=".$password;

$cookie_file_path = $path."/cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/page/");
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
curl_close($ch);

更新:这段代码从来就不是复制粘贴的。这是为了展示我如何在我的特定用例中使用它。您应该根据需要使其适应您的代码。例如目录、变量等

这就是我在ImpressPages中解决此问题的方法:

//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp');  //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

巴拿马杰克示例对我不起作用-给出致命错误:调用未定义的函数build_unique_path()。我使用了以下代码-(更简单-我的意见):



$login\u email='alabala@gmail.com';
$login_pass='alabala4807'
$cookie\u file\u path=“/tmp/cookies.txt”
$LOGINURL=”http://alabala.com/index.php?route=account/login";
$agent=“诺基亚通讯器WWW-Browser/2.0(Geos 3.0 Nokia-9000i)”

$ch=curl_init()

$headers[]=“接受://*”
$headers[]=“连接:保持活动状态”

//所有请求的基本curl选项
curl\u setopt($ch,CURLOPT\u HTTPHEADER,$headers)
curl_setopt($ch,CURLOPT_头,0)
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0)
curl\u setopt($ch,CURLOPT\u SSL\u VERIFYPEER,false)
curl_setopt($ch,CURLOPT_USERAGENT,$agent)
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1)
curl\u setopt($ch,CURLOPT\u COOKIEFILE,$cookie\u file\u path)
curl\u setopt($ch,CURLOPT\u COOKIEJAR,$cookie\u file\u path)

//设置第一个URL
curl\u setopt($ch,CURLOPT\u URL,$LOGINURL)

//执行会话以获取cookie和所需的表单输入
$content=curl\u exec($ch)

//从登录所需的表单中获取隐藏的输入
$fields=getFormFields($content)
$fields['email']=$login\u email
$fields['password']=$login\u pass

//使用我们从表单中提取的内容设置postfields
$postfields=http\u build\u query($fields)
//将URL更改为登录URL
curl_setopt($ch,CURLOPT_URL,$LOGINURL)

curl\u setopt($ch,CURLOPT\u post,1)
curl_setopt($ch,CURLOPT_POSTFIELDS,$POSTFIELDS)

//执行登录
$result=curl\u exec($ch)

打印$result
(“/(]+>)/is“,$form,$matches);
如果($elements>0){
对于($i=0;$i$el=preg\u replace('/\s{2,}/',''.$matches[1][$i]);
如果(preg\u match('/name=(?:[“\''.*))/i',$el,$name)){
$name=$name=$name[1];
$value=
('/value=(?:[“\'])?([^“\'\s]*)/i',$el,$value)){
$value=$value[1];
}



$inputs[$name]=$value;
}



返回$inputs;
}

$grab\u url='1http://grab.url/alabala“;

页面包含我想要抓取的内容
curl_setopt($ch,CURLOPT_URL,$grab_URL);
使用DomDocument()等
$html=curl_exec($ch);
curl_close($ch);

var_dump($html)
死亡;

你的
$url
应该是gmail登录的,而不是你自己的脚本。那是登录页面。它是登录表单所在的位置。我不想用这个脚本登录gmail。你确保cookie.txt可以用你的脚本写吗?你如何确保给予正确的权限?@pattyd I提供并接受d我的工作解决方案的答案//因为一些站点在第二页重定向并设置cookie,可能需要这样做:curl\u setopt($ch,CURLOPT\u FOLLOWLOCATION,1);我如何检查我是否已经登录?(跳过发送登录请求)@用户3383675你是什么意思?使用curl尝试登录到一个页面,检查你是否已登录,然后做一些其他事情?(有一个请求?)我的意思是:while(一些条件){isLoggedIn()?doSomething():login()&&doSomething()}这是每次迭代前登录的必要条件,还是我只能发送一次登录请求,然后在下一次迭代中不登录就可以了?这个答案对我很有效,但有一个例外:在使用POST登录后,您忘记了更改回去获取受保护的文件下载。在某些服务器上(在我的情况下)它可能会阻止您获取该文件。在第二个exec为我修复它之前,简单的
curl\u setopt($ch,CURLOPT\u POST,0);
。这很好,谢谢
define("DOC_ROOT","/path/to/html");
//username and password of account
$username = trim($values["email"]);
$password = trim($values["password"]);

//set the directory for the cookie using defined document root var
$path = DOC_ROOT."/ctemp";
//build a unique path with every request to store. the info per user with custom func. I used this function to build unique paths based on member ID, that was for my use case. It can be a regular dir.
//$path = build_unique_path($path); // this was for my use case

//login form action url
$url="https://www.example.com/login/action"; 
$postinfo = "email=".$username."&password=".$password;

$cookie_file_path = $path."/cookie.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/page/");
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
curl_close($ch);
//initial request with login data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/login.php');
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=XXXXX&password=XXXXX");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name');  //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/ip4.x/file/tmp');  //could be empty, but cause problems on some hosts
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

//another request preserving the session

curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/profile');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$answer = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}

// options
$login_email = 'alabala@gmail.com';
$login_pass = 'alabala4807';
$cookie_file_path = "/tmp/cookies.txt";
$LOGINURL = "http://alabala.com/index.php?route=account/login";
$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['email'] = $login_email;
$fields['password'] = $login_pass;

// 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('/()/is', $data, $matches)) {
$inputs = getInputs($matches[1]);

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

function getInputs($form)
{
$inputs = array();
$elements = preg_match_all("/(]+>)/is", $form, $matches);
if ($elements > 0) {
for($i = 0;$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;
}

$grab_url='http://grab.url/alabala';

//page with the content I want to grab
curl_setopt($ch, CURLOPT_URL, $grab_url);
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
curl_close($ch);

var_dump($html);
die;