简体   繁体   中英

How can I use current site cookies to login to another site and then redirect to another page and make a CURL request?

How can I use current site cookies to login to another site and then redirect to another page ?

I have multiple websites which uses the same external Authenticator.I am trying to make a CURL request from one site to get data from the other site . But I have authentication issue while making the CURL request to other site.

In order to login to another site I need to click on login URL but credentials need not be entered again.

So My actual requirement is :

  1. Use Cookies from current site.

  2. Make a CURL request to another site login URL and login to that site using Cookies from current site.

  3. Make a CURL request to another URL where you can obtain the required data.

I can obtain data from the other site if I am logged into that site in the browser.

My Current Code is as follows:

$agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
$loginURL = $url . 'saml_login';
$cleanedURL = $url . 'anotherurl/list';

$curl = curl_init();

//Before making CURl request to $cleanedURL I need to make request to $loginURL(which has a redirect)

curl_setopt($curl, CURLOPT_URL, $cleanedURL);

curl_setopt($curl, CURLOPT_USERAGENT, $agent);

curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Cookie: ' . $_SERVER['HTTP_COOKIE']));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);

curl_setopt($curl, CURLOPT_VERBOSE, TRUE);

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);

$response = curl_exec($curl);

var_dump($response);

All the examples I came across used either used credentials in the header or cookies file which is already exported.

Can any one help me to suggest it.

Cookie sharing is possible by domain. If your application at app.domain.com register a cookie for wildcard domain.com instead of app.domain.com, any subdomain.domain.com will have the access to the cookie key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM