I left it for a long time, but returned to it later. Since this issue is reviewed regularly. In the end, this is what I used, and it worked for me.
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);
Update: this code was never intended to be copied and pasted. This was to show how I used it for my particular use case. You must adapt it to your code as needed. Such as directories, variables, etc.
Panama Jack Jan 16 '14 at 19:03 2014-01-16 19:03
source share