php 远程登陆,使用PHP 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);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值