php - 登录到 PopAds.net

标签 php curl

我已经搜索过这个网站和谷歌,但似乎无法弄清楚如何让它发挥作用。我正在尝试使用 PHP 脚本登录到 popads.net,这样我就可以将我的网站收入汇总到一个页面上。但是这个网站给我带来了麻烦。谁能看出我做错了什么?

<?php
//username and password of account
$username = 'myusername';
$password = 'mypassword';

//set the directory for the cookie using defined document root var
$path = DOC_ROOT."/ctemp";

//login form action url
$url="https://www.popads.net/users/login"; 
$postinfo = "data[User][username]=".$username."&data[User][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, "https://www.popads.net/users/dashboard");
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
echo $html;
curl_close($ch);

最佳答案

由于此站点使用 CSRF 保护,为了登录,您需要首先从原始表单中获取 CSRF token ,并将其与登录数据一起传递到登录端点。 CSRF token 在主页上具有字段名称 data[_Token][key]。该站点在设置时也可能会设置一个 cookie,因此如果您从 cURL 获取该 cookie 数据,您将需要传回该 cookie 数据。

这就是说:我的建议是查看他们是否有官方 API,并且在自己编写爬虫代码之前,请确保您没有违反任何可能使您被列入黑名单的条款和条件。

关于php - 登录到 PopAds.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084543/

相关文章:

php - Laravel-5.6 'LIKE' 在选择的地方

javascript - AddThis 工具箱多个项目

PHP:CURLOPT_URL 的真正作用是什么?

PHP调用类方法/函数

php - 可以创建一个包含 MySQL 表数据的溢出表

php - 如何从字符串中获取关联数组?

php - 如何将 IBM Watson cURL 命令转换为 PHP

curl - Solr 通过 CURL 查询 - [globbing] 列中的错误范围

curl - 如何通过curl查询Logstash并仅返回特定字段

web-services - 如何测试存在时间滞后的 Web 服务?