5

我正在尝试使用登录安全但无法做到的情况下抓取站点的内容该站点的登录有三个选项用户名,密码,密码这里是我正在使用的代码

<?php

// HTTP authentication

$url = "http://aftabcurrency.com/login_script.php";

$ch = curl_init();    

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_URL, $url); 
$cookie = 'cookies.txt';
$timeout = 30;
curl_setopt($curl, CURLOPT_TIMEOUT,         10); 
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,  $timeout );
curl_setopt($curl, CURLOPT_COOKIEJAR,       $cookie);
curl_setopt($curl, CURLOPT_COOKIEFILE,      $cookie);

curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch,CURLOPT_POSTFIELDS,"user_name=user&user_password=pass&passcode=code");             

$result = curl_exec($ch); 

curl_close($ch); 

echo $result;

?>
4

2 回答 2

7

您需要对POSThttp://aftabcurrency.com/login_script.php进行操作, 您的 curl 还需要接受 cookie。
身份验证后,脚本将重定向您,因此您还需要添加CURLOPT_FOLLOWACTION

这是您的脚本的编辑版本,我无法在http://aftabcurrency.com/上对其进行测试,希望它有效:

$url = "http://aftabcurrency.com/login_script.php";

$ch = curl_init();    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_URL, $url); 
$cookie = 'cookies.txt';
$timeout = 30;

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,         10); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,  $timeout );
curl_setopt($ch, CURLOPT_COOKIEJAR,       $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE,      $cookie);

curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch,CURLOPT_POSTFIELDS,"user_name=user&user_password=pass&passcode=code");     

$result = curl_exec($ch);

/* //OPTIONAL - Redirect to another page after login
$url = "http://aftabcurrency.com/some_other_page";
curl_setopt ($ch, CURLOPT_POST, 0); 
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
 */ //end OPTIONAL 

curl_close($ch); 
echo $result;
于 2012-06-23T18:10:59.333 回答
0

您需要将您的用户名/密码/密码发布到该页面。您现在正在尝试做的是 http 身份验证。所以代替这个

curl_setopt($ch, CURLOPT_USERPWD, "demo:demopass:demopasscode"); 

你需要这个

curl_setopt ($ch, CURLOPT_POST, 1); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, "user_name=xxxxx&user_password=xxxxxx&passcode=xxxxx"); 
于 2012-06-23T18:15:15.953 回答