简体   繁体   中英

PHP - Using cURL to get a cookie from a redirecting URL but getting no response

I have a cURL command using which I get authenticated to a website and it gives a cookie in response, then subsequently using this cookie I can make REST API calls to this service

Here is the working cURL command:

curl -v -l -H "Content-Type: application/x-www-form-urlencoded" -H "Referrer:https://mywebsiteurl.com?ticket=unique_ticket_id" -d "_charset_=UTF-8&errorMessage=User+name+and+password+do+not+match&resource=%2F&username=username%40domain.com&password=XXXXXX&nextpage=welcomeCM.jsp&viewInfo=&ticket=unique_ticket_id"  -X  POST https://mywebsiteurl.com/index.jsp

In the above command ticket parameter contains a unique ticket id that is passed along with the website url

Now, I'm trying to accomplish the same using cURL PHP, here is the PHP code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/index.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "_charset_=UTF-8&errorMessage=User+name+and+password+do+not+match&resource=%2F&username=username%40domain.com&password=XXXXXX&nextpage=welcomeCM.jsp&viewInfo=&ticket=unique_ticket_id");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Referrer: https://mywebsiteurl.com?ticket=unique_ticket_id'
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

echo $result;

curl_close ($ch);

Here echo $result shows nothing

I then tried var_dump($result); and it gives this output: string(0) "" which means nothing is getting returned in $response

After this I tried curl_getinfo and added the following code:

echo "<pre>";
$info = curl_getinfo($ch);
print_r($info);
echo "</pre>";

And this gave me the following array:

Array
(
    [url] => https://mywebsiteurl.com/index.jsp
    [content_type] => text/html;charset=UTF-8
    [http_code] => 302
    [header_size] => 1306
    [request_size] => 619
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.540687
    [namelookup_time] => 0.028262
    [connect_time] => 0.171774
    [pretransfer_time] => 0.462507
    [size_upload] => 280
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 181
    [download_content_length] => 0
    [upload_content_length] => 280
    [starttransfer_time] => 1.54066
    [redirect_time] => 0
    [redirect_url] => https://mywebsiteurl.com/welcomeCM.jsp?username=username@domain.com&locale=en_US
    [primary_ip] => YY.YYY.YYY.YY
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => XX.XX.XXX.XX
    [local_port] => 47692
)

Now my goal is to get the cookie in $result , but it is empty & nothing is getting returned into it

Is it that something is missing in the cURL command equivalent PHP code?

Can someone please help me out & point me in the direction to retrieve the cookie

Thanks a lot!

UPDATE

I added curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); to the request to have it follow the 302 redirect after successful login

But this time after the execution of cURL, echo $result is redirecting my current local webpage to /Dashboard?viewInfo= which obviously does not exist

Also this time var_dump($result); is resulting in: string(1462) " "

And curl_getinfo($ch) this time is giving the following array:

Array
(
    [url] => https://mywebsiteurl.com/welcomeCM.jsp?username=username@domain.com&locale=en_US
    [content_type] => text/html;charset=UTF-8
    [http_code] => 200
    [header_size] => 1821
    [request_size] => 956
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 1.746911
    [namelookup_time] => 1.5E-5
    [connect_time] => 1.5E-5
    [pretransfer_time] => 6.2E-5
    [size_upload] => 0
    [size_download] => 1462
    [speed_download] => 836
    [speed_upload] => 0
    [download_content_length] => 1462
    [upload_content_length] => 0
    [starttransfer_time] => 0.146587
    [redirect_time] => 1.6003
    [redirect_url] => 
    [primary_ip] => YY.YYY.YYY.YY
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => YY.YY.YYY.YY
    [local_port] => 47705
)

Still can't get the required cookie here, Please help!

If you want to use curls given functionality you can try using CURLOPT_COOKIEJAR / CURLOPT_COOKIEFILE, to store and get your cookie. This if file based. In this case, curl will then write any Cookie related Information in a file.

You can define where this file will be located.

By using CURLOPT_COOKIEJAR , you will tell curl that it shall capture the cookie data.

// create cookie file
$cookie = __DIR__ . "/where/you/want/to/store/your/cookie/mycookie.txt";
fopen($cookie, "w");
// you may want to use some random identicator in your cookie file
// to make sure it does not get overwritten by some action

// prepare login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/index.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// tell curl to follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURlOPT_POSTFIELDS, "username=usernam&password=XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
// set a jar, to store your cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Referrer: https://mywebsiteurl.com?ticket=unique_ticket_id'
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// do login
$loginResult = curl_exec($ch);

// check if your login has worked according to the given result by any method you want
if (preg_match("/Welcome/", $loginResult)) {
    // simple regex for demonstration purpose
}

// now you could load your cookie out of the file, or just use the file for future requests

If your login has worked, you will have all information stored in your cookie file.

Now you can use that cookie again with the curlopt CURLOPT_COOKIEFILE for future requests.

// do other request, with your cookie
curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/api/rest.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "your new post data");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// set a jar to update your cookie if needed
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
// give curl the location of your cookie file, so it can send it
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

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