2010-06-09 159 views
49

我刚刚使用cURL和它很难找到它的好资源。 我想要做的是登录到远程站点,通过卷曲做登录表单,然后发回它是成功的。用PHP cURL登录到远程站点

我的代码似乎并不工作,只试图显示该网站的主页。

$username="[email protected]"; 
$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); 

我在做什么错。这是工作后,我想重定向到另一个页面,并从我的网站获取内容。

+0

你确定cookie.txt可以通过你的脚本写吗? – Mark 2010-06-09 20:23:36

+0

你的'$ url'应该是gmail登录名,而不是你自己的脚本。 – 2010-06-09 18:56:49

+0

这是登录页面。这是登录表单所在的位置。我没有试图用这个脚本登录到Gmail。 – 2010-06-09 19:12:08

回答

39

我已经让它变好了一段时间,但稍后重新访问它。由于这个问题被定期查看。这最终是我最终使用的为我工作的。

//username and password of account 
$username = trim($values["email"]); 
$password = trim($values["password"]); 

//set the directory for the cookie using defined document root var 
$dir = DOC_ROOT."/ctemp"; 
//build a unique path with every request to store 
//the info per user with custom func. 
$path = build_unique_path($dir); 

//login form action url 
$url="https://www.site.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.site.com/page/"); 
//do stuff with the info with DomDocument() etc 
$html = curl_exec($ch); 
curl_close($ch); 
+0

+1是的,这是正确的,如果您想在登录后执行一些其他请求,例如获取用户的收件箱消息(登录后),您应该像我的回答那样做。请检查它 – ncm 2014-01-16 19:12:35

+0

@imsiso是的,在我的实际代码中,我导航了几个URL以获取其他数据。 – 2014-01-16 19:19:02

+0

我可以在twitter上使用它吗? – sotirios 2015-12-14 22:58:53

14

查看登录页面的来源。寻找formHTML标记。在该标签内的内容看起来像action=将该值用作$url,而不是表单本身的URL。

此外,当你在那里,验证输入框被命名为你有他们列出。

例如,一个基本的登录表单将类似于:使用

<form method='post' action='postlogin.php'> 
    Email Address: <input type='text' name='email'> 
    Password: <input type='password' name='password'> 
</form> 

上述形式为例,更改到$url值:

$url="http://www.myremotesite.com/postlogin.php"; 

确保你使用的值列在$postdata

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

它应该只是fin即

16

我有同样的问题,我发现这个答案on this website

而且我改变它只是一点点(在最后一行curl_close)

$username = 'myuser'; 
$password = 'mypass'; 
$loginUrl = 'http://www.example.com/login/'; 

//init curl 
$ch = curl_init(); 

//Set the URL to work with 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 

// ENABLE HTTP POST 
curl_setopt($ch, CURLOPT_POST, 1); 

//Set the post parameters 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password); 

//Handle cookies for the login 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 

//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL 
//not to print out the results of its query. 
//Instead, it will return the results as a string return value 
//from curl_exec() instead of the usual true/false. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

//execute the request (the login) 
$store = curl_exec($ch); 

//the login is now done and you can continue to get the 
//protected content. 

//set the URL to the protected file 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/protected/download.zip'); 

//execute the request 
$content = curl_exec($ch); 

curl_close($ch); 

//save the data to disk 
file_put_contents('~/download.zip', $content); 

我认为这是你是对的看for.Am我?


和一个有用的相关问题。关于如何在cUrl中保持活动状态:https://stackoverflow.com/a/13020494/2226796

+0

谢谢我更新了答案。 – 2014-01-16 19:07:29

+0

很容易为我工作,谢谢! – 2015-12-10 07:08:10

+0

我如何检查我是否已经登录? (跳过发送登录请求) – user3383675 2016-02-04 10:32:41

9

这是我如何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); 
} 
+0

//由于某些网站在第二页重定向并设置了cookie,因此可能需要: curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1); – 2014-11-15 22:54:38

0

巴拿马杰克例不适合我的工作 - 给致命错误:调用未定义功能build_unique_path()。我用这个代码 - (更简单 - 我的意见):


// options
$login_email = '[email protected]';
$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;

+0

**我的回答不是复制和粘贴**。这只是我用过的一个例子。唯一相关的部分是** curl **代码。 'build_unique_path'是我创建的一个自定义函数,我在该示例中注释到了这一点。答案是创建自己的指南。 – 2017-07-08 07:34:26

+0

好的男人,不要判断你,只是你的榜样不适合我,没有什么个人的,我只是分享一个适合我的例子。是的,这不是我的代码,但我的工作是测试和格式化示例 – Pavel 2017-07-10 12:34:26