PHP cURL Location Change PHP cURL Location Change curl curl

PHP cURL Location Change


You probably need to save and transmit cookies. This can be done in PHP cURL like this:

$ch = curl_init();// set your regular optionscurl_setopt($ch, CURLOPT_URL, 'http://somedomain.com/login.php');curl_setopt($ch, CURLOPT_POSTFIELDS, array('user'=>'foo', 'pass'=>'bar'));// set where cookies are savedcurl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');// set where cookies are retrieved from when sent to the servercurl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');// execute logincurl_exec($ch);// do another requestcurl_setopt($ch, CURLOPT_URL, 'http://somedomain.com/restricted_page.php');curl_setopt($ch, CURLOPT_HTTPGET, 1);curl_exec($ch);


You need to handle cookies. They're needed for the session authentication.

Get the Cookie header from the first call and send it with all subsequent calls.


You must know how login pages (usually) work. When you log into a site the site will send you a session cookie that you will send again to the website everytime you request a new page. So if you want to emulate the login you must store cookies returned by the login and then include them in every other request to the site.