Php cURL登录到Linkbucks
我正在尝试制作一个脚本,登录到我的Linkbucks帐户以获取我当前的统计数据 他们提供和api,但只是为了创建链接,我需要的是你得到的统计数据 我发现的事情:Php cURL登录到Linkbucks,php,curl,login,Php,Curl,Login,我正在尝试制作一个脚本,登录到我的Linkbucks帐户以获取我当前的统计数据 他们提供和api,但只是为了创建链接,我需要的是你得到的统计数据 我发现的事情: 首先,你必须保持记录 为了获得统计数据,该网站使用类似于以下内容的JSON帖子对:进行ajax调用:{“month”:“09/01/2015”} 有了这篇文章,我很容易得到我需要的信息,问题是我的脚本不起作用 我和你共享代码,所以请帮助我 任何想法或解决方案,或任何将受到赞赏 这是我的剧本: <?php $urlLogin =
<?php
$urlLogin = "https://www.linkbucks.com/Default.aspx";
$ch = getSource($urlLogin);
$fuente = curl_exec($ch);
$re = "/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $matches);
$re = "/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $validation);
$re = "/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $generator);
$post = array(
"ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Username" => "yourusername" ,
"ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Password" => "yourpassword" ,
"__VIEWSTATE" => $matches[1] ,
"__VIEWSTATEGENERATOR" => $generator[1] ,
"__EVENTVALIDATION" => $validation[1]
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$config['useragent'] = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return $ch;
}
function postData($url , $array) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$server_output = curl_exec($ch);
return ($server_output);
}
?>
这应该可以:
$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source = getSource($urlLogin, $useragent);
$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source);
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source);
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source);
$post = array(
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourUsername",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourPassword",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0,
"__VIEWSTATE" => $viewstate,
"__VIEWSTATEGENERATOR" => $generator,
"__EVENTVALIDATION" => $validation
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $useragent = null, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return curl_exec($ch);
}
function postData($url, $postArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
return $server_output;
}
function findStringByRegex($regex, $source) {
preg_match($regex, $source, $matches);
return $matches[1];
}
$urlogin=”https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent='Mozilla/5.0(X11;Linux i686)AppleWebKit/534.33(KHTML,比如Gecko)Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source=getSource($urlLogin,$useragent);
$viewstate=findStringByRegex(“/”,$source);
$generator=findStringByRegex(“//”,$source);
$validation=findStringByRegex(“/”,$source);
$post=数组(
“ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username”=>“yourUsername”,
“ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password”=>“您的密码”,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LOGINIGBTN.x'=>0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LOGINIGBTN.y'=>0,
“_VIEWSTATE”=>$VIEWSTATE,
“\uu VIEWSTATEGENERATOR”=>$generator,
“\uu EVENTVALIDATION”=>$validation
);
$data=postData($urlLogin,$post);
回波数据;
函数getSource($url,$useragent=null,$header=null){
$ch=curl\u init($url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,$USERAGENT);
curl\u setopt($ch,CURLOPT\u REFERER,(是否为null($header)?”https://www.google.com/“:$header”);
返回curl_exec($ch);
}
函数postData($url,$postArray){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postArray);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie.txt');
curl_setopt($ch,CURLOPT_REFERER,”https://www.linkbucks.com/Default.aspx");
$server\u output=curl\u exec($ch);
返回$server\u输出;
}
函数findStringByRegex($regex,$source){
preg_match($regex,$source,$matches);
返回$matches[1];
}
我清理了一下你的代码,基本上都很好,你只是缺少了两个post字段:ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LOGINIGBTN.x和ctl00$ctl00$PHMENUBAR$ctl00$LOGINIGBTN.y’最后,多亏了runz0rd我才能够让代码正常工作 非常感谢大家 以下是代码的最终版本:
<?php
$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source = getSource($urlLogin, $useragent);
$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source);
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source);
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source);
$post = array(
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourusername",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourpassword",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0,
"__VIEWSTATE" => $viewstate,
"__VIEWSTATEGENERATOR" => $generator,
"__EVENTVALIDATION" => $validation
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $useragent = null, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return curl_exec($ch);
}
function postData($url, $postArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArray));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$fecha = json_encode( array( "month"=> "09/01/2015" ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fecha );
$server_output = curl_exec($ch);
return $server_output;
}
function findStringByRegex($regex, $source) {
preg_match($regex, $source, $matches);
return $matches[1];
}
?>
您遇到了什么错误?我没有遇到任何错误,只是代码不起作用,但现在已经解决了!!非常感谢你!!!我已经用POST信息完成了它,以获得JSON响应,它工作正常。我真的很感激,真的,谢谢你:)