Mohammad Mohammad - 3 months ago 27
PHP Question

Get cookie from another website with cUrl

How can I get cookie from another website with cUrl?

I want to get cookie of this https://ais.usvisa-info.com/en-ae/niv/users/sign_in site!

For example:

Cookie:
_ga=GA1.2.1101352908.1469077182; _yatri_session=TTNTOWhoQjJoTHVNNkVnQ29XOGxqVUZxQzJXYWpBZjhOQWhaSFVnQlhqUDhSLytCelA2K2UvSEl1TG1MUmRDL2JUNi95b1pRa2pJVGpGdmxGWmh6dWI0ckppOVVoUnNObWJjLyt0MFF2bmppeGJLNEVxNFdaODBQQWhPb3VrSlh3bTJoWGVpTFdwSTJxRFpPNVRSeTBlZHg3bWR1WnVKZk0rMWRCeHYxSnFESG5EVVJlRnRJbytGZUl4SzAzYjdkWnoySFcySkt1MW53UlpJU21mcGpROHppVHdJblJmWDB4clFTTHRtR1BtM29lVUtqMWtBaERLeVp5d094SFBhYldyR1NvTHR5dFBGMDdCYVlhcWdiM0ttSzFCRWxpNHZMaktKMEpGd01Ub0p3enpVZFVLSDBrb2VhdmNTWDJOazJ0MkFCemVpci85aWZKS1JPczhiaml2eEQ1YTYzZVUzcDJ5K2dRYUk2dnAxUnNTTk5yMkpaY2VOVWJCRUM5bERlLS1ITFFaK05wNWFmaDhUOTZlYVhlbk5RPT0%3D--dc9cf7a71467b9d93e656e087ccf4a3841304cb5

Answer

Check the below code:

<?php
    // open a site with cookies
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://ais.usvisa-info.com/en-ae/niv/users/sign_in");
    curl_setopt($ch, CURLOPT_HEADER  ,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $content = curl_exec($ch);

    // get cookies
    $cookies = array();
    preg_match_all('/Set-Cookie:(?<cookie>\s{0,}.*)$/im', $content, $cookies);

    echo $cookies['cookie'][0];

?>