栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用PHP cURL登录到远程站点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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 optionalcurl_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 grabcurl_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等



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442948.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号