Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:41:34+00:00 2026-05-31T17:41:34+00:00

I am connecting to an API service which authenticates users using cookies. I make

  • 0

I am connecting to an API service which authenticates users using cookies.
I make these two statements from command prompt and it works.

curl -d "u=username&p=password" -c ~/cookiejar https://domain/login

curl -b https://domain/getData

Now I want to make two equivalent php files login.php and get_data.php using curl.

I am using

curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);

in login.php

and

curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

in get_data.php

It is not working. Cookie file is getting created but second curl is not reading it.
Is this the right way to do it ? Do I have to read the cookie file seperately and set the header Cookie ? Any help would appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:41:35+00:00Added an answer on May 31, 2026 at 5:41 pm

    This will do the trick. I run it against Google.com as an example:

    <?PHP
    
    // open a site with cookies
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
    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);
    
    print_r($cookies['cookie']); // show harvested cookies
    
    // basic parsing of cookie strings (just an example)
    $cookieParts = array();
    preg_match_all('/Set-Cookie:\s{0,}(?P<name>[^=]*)=(?P<value>[^;]*).*?expires=(?P<expires>[^;]*).*?path=(?P<path>[^;]*).*?domain=(?P<domain>[^\s;]*).*?$/im', $content, $cookieParts);
    print_r($cookieParts);
    
    ?>
    

    See other examples for how to effectively parse such as string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm interested in connecting to the Evernote Service API, which uses Thrift, from a
Am doing a GET(REST) and using InternetReadFile API to read response which is an
I've been asked to look at windows service which retrieves data from an Active
I am connecting to an ODATA Service via a C# ASP.NET application, which has
I'm using a vendor API to obtain a JDBC connection to the application's database.
I have been using the mysql api in PHP, and am now converting to
When connecting to a network share for which the current user (in my case,
I have someone connecting to my repository using the url (substituted the IP address):
I'm connecting to a web service hosted by a third-party provider. I've added a
I am connecting to a 3rd party API. They have provided me with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.