I'm new to using cURL and its hard to find good resources for it. What I'm trying to do is login to a remote site, by having curl do the login form and then send back that it was successful.
The code I have doesn't seem to work and only tries to show the main page of the site.
$username="[email protected]"; $password="mypassword"; $url="http://www.myremotesite.com/index.php?page=login"; $cookie="cookie.txt"; $postdata = "email=".$username."&password=".$password; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt ($ch, CURLOPT_TIMEOUT, 60); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt ($ch, CURLOPT_REFERER, $url); curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt ($ch, CURLOPT_POST, 1); $result = curl_exec ($ch); echo $result; curl_close($ch);
What am I doing wrong. After this is working I want to redirect to another page and get content from my site.
For example, if a website has protected content curl allows you to pass authentication credentials. To do so use the following syntax: curl --user "USERNAME:PASSWORD" https://www.domain.com . “USERNAME” must be replaced with your actual username in quotes.
cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.
php file with the following contents. $url = 'https://www.example.com' ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url );
php $url = 'hxxp://domain.com/univ/v8?q=tas+wanita'; $ch=curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $r=curl_exec($ch); curl_close($ch); print_r(json_decode($r, true)); ?>
I had let this go for a good while but revisited it later. Since this question is viewed regularly. This is eventually what I ended up using that 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 meant to be a copy and paste. It was to show how I used it for my specific use case. You should adapt it to your code as needed. Such as directories, vars etc
I had same question and I found this answer on this website.
And I changed it just a little bit (the curl_close at last line)
$username = 'myuser'; $password = 'mypass'; $loginUrl = 'http://www.example.com/login/'; //init curl $ch = curl_init(); //Set the URL to work with curl_setopt($ch, CURLOPT_URL, $loginUrl); // ENABLE HTTP POST curl_setopt($ch, CURLOPT_POST, 1); //Set the post parameters curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password); //Handle cookies for the login curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL //not to print out the results of its query. //Instead, it will return the results as a string return value //from curl_exec() instead of the usual true/false. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //execute the request (the login) $store = curl_exec($ch); //the login is now done and you can continue to get the //protected content. //set the URL to the protected file curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/protected/download.zip'); //execute the request $content = curl_exec($ch); curl_close($ch); //save the data to disk file_put_contents('~/download.zip', $content);
I think this was what you were looking for.Am I right?
And one useful related question. About how to keep a session alive in cUrl: https://stackoverflow.com/a/13020494/2226796
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With