<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

      <title>Comments on: How do you handle authentication via cookie with CURL?</title>
      <link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL/</link>
      <description>Comments on Ask MetaFilter post How do you handle authentication via cookie with CURL?</description>
	  	  <pubDate>Thu, 19 May 2005 19:30:15 -0800</pubDate>
      <lastBuildDate>Thu, 19 May 2005 19:30:15 -0800</lastBuildDate>
      <language>en-us</language>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <ttl>60</ttl>

<item>
  	<title>Question: How do you handle authentication via cookie with CURL?</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL</link>	
  	<description>how do you handle authentication via cookie with CURL? I&#8217;ve been assigned a tedious job at work. I wrote a PHP script to do it for me, but it requires CURL, which I&#8217;ve never used before. Basically I have to replace text in a file with text from various sources. I&#8217;m uploading a set of files to my webhost (company doesn&#8217;t use PHP), running the script and outputting them to a separate file. The problem is one of the sources I&#8217;m getting data from is the company website, which is password protected. I have a password and username, but I&#8217;m not sure how CURL deals with them. Once they&#8217;ve been entered, a cookie is stored to save auth status.&lt;br&gt;
&lt;br&gt;
Do I just use http://name:passwd@machine.domain/full/path/to/file and let CURL handle everything? Or do I have to tell it how to handle the cookie? Or am I not getting this at all?&lt;br&gt;
&lt;br&gt;
Thanks.</description>
  	<guid isPermaLink="false">post:ask.metafilter.com,2008:site.18923</guid>
  	<pubDate>Thu, 19 May 2005 18:57:12 -0800</pubDate>
  	<dc:creator>miniape</dc:creator>
	
	<category>php</category>
	
	<category>curl</category>
	
</item>
<item>
  	<title>By: nicwolff</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312590</link>	
  	<description>If you mean the username and password are entered in a form on a login page, then cURL can &amp;quot;submit&amp;quot; that form like:&lt;br&gt;
&lt;br&gt;
curl -d &amp;quot;username=miniape&amp;amp;password=SeCrEt&amp;quot; http://whatever.com/login&lt;br&gt;
&lt;br&gt;
and if you want to store the cookie that comes back you do so by specifying a cookie file:&lt;br&gt;
&lt;br&gt;
curl -c cookies.txt -d &amp;quot;username=miniape&amp;amp;password=SeCrEt&amp;quot; http://whatever.com/login&lt;br&gt;
&lt;br&gt;
and to use those cookie in later requests you do:&lt;br&gt;
&lt;br&gt;
curl -b cookies.txt -d &amp;quot;username=miniape&amp;amp;password=SeCrEt&amp;quot; http://whatever.com/login&lt;br&gt;
&lt;br&gt;
or do both if you want to both send and receive cookies:&lt;br&gt;
&lt;br&gt;
curl -b cookies.txt -c cookies.txt -d &amp;quot;username=miniape&amp;amp;password=SeCrEt&amp;quot; http://whatever.com/login&lt;br&gt;
&lt;br&gt;
&lt;small&gt;By the way, I learned all this in the last couple of minutes by Googling for &amp;quot;curl&amp;quot;. Just sayin&apos;.&lt;/small&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312590</guid>
  	<pubDate>Thu, 19 May 2005 19:30:15 -0800</pubDate>
  	<dc:creator>nicwolff</dc:creator>
</item>
<item>
  	<title>By: nicwolff</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312591</link>	
  	<description>&lt;small&gt;Sorry, &lt;a href=&quot;http://curl.haxx.se/docs/httpscripting.html&quot;&gt;here&apos;s the page I found&lt;/a&gt;.&lt;/small&gt;</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312591</guid>
  	<pubDate>Thu, 19 May 2005 19:31:54 -0800</pubDate>
  	<dc:creator>nicwolff</dc:creator>
</item>
<item>
  	<title>By: nicwolff</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312592</link>	
  	<description>Sorry again, I fucked that all up. Well not all, the first request is fine, but you don&apos;t need the CGI data once you have the cookie, so it&apos;s just:&lt;br&gt;
&lt;br&gt;
curl -b cookies.txt http://whatever.com/content&lt;br&gt;
&lt;br&gt;
on the later requests.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312592</guid>
  	<pubDate>Thu, 19 May 2005 19:33:59 -0800</pubDate>
  	<dc:creator>nicwolff</dc:creator>
</item>
<item>
  	<title>By: zsazsa</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312593</link>	
  	<description>Does the browser pop up an authenticaion dialog like &lt;a href=&quot;http://polpo.org/tmp/httpauth.png&quot;&gt;this one&lt;/a&gt; &lt;small&gt;(FireFox above, IE below)&lt;/small&gt;, or is the login integrated to the webpage with a submittable form?  If it&apos;s the former, the http://name:passwd@ method will work with no problems.  If it&apos;s the latter, you&apos;ll have to use a cookie, which is more complicated.  curl can get cookies from a standard &amp;quot;cookies.txt&amp;quot; type file like those generated by Firefox.  So you&apos;d have to visit the site, log in, find your cookies.txt file, isolate the lines having to do with the site, and adjust the expiry time so the cookie doesn&apos;t expire. If you&apos;re using curl from inside PHP, use the &lt;a href=&quot;http://us4.php.net/curl&quot;&gt;CURLOPT_COOKIEJAR&lt;/a&gt; option to point to the cookies.txt file you made.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312593</guid>
  	<pubDate>Thu, 19 May 2005 19:35:26 -0800</pubDate>
  	<dc:creator>zsazsa</dc:creator>
</item>
<item>
  	<title>By: cillit bang</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312660</link>	
  	<description>Login using a normal browser and use the &amp;quot;Show Cookies&amp;quot; (or whatever) option to find out what cookies were set. Then use either:&lt;br&gt;
&lt;br&gt;
curl [URL] -H &apos;Cookie: cookie1=value; cookie2=value, etc&apos;&lt;br&gt;
or:&lt;br&gt;
curl_setopt($curl,array(&apos;Cookie: cookie1=value; cookie2=value, etc&amp;quot;));&lt;br&gt;
&lt;br&gt;
to include the cookies in the request.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312660</guid>
  	<pubDate>Fri, 20 May 2005 02:01:55 -0800</pubDate>
  	<dc:creator>cillit bang</dc:creator>
</item>
<item>
  	<title>By: eriko</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312714</link>	
  	<description>My day job involves, amongst many other things, sending out insurance claims to just about every variant of BCBS, Medicare, Medicaid, and a bunch of private insurers. They all do things differently. Many of them (the Medicaids, in particular), use web connections. Hates them, I do....&lt;br&gt;
&lt;br&gt;
The best tool for web connections is curl. But you need to read headers, to figure out what to tell curl to send. So, you need something like &lt;a href=&quot;http://livehttpheaders.mozdev.org&quot;&gt;this&lt;/a&gt; -- which will let you watch the HTTP transaction while you do things by hand.&lt;br&gt;
&lt;br&gt;
Defaults: Curl command lines can get long. Create a &amp;quot;COMMONSWITCH&amp;quot; variable for the ones you&apos;ll use in every connection, since you will almost certainly need to make more than one in a given transaction. My typical setting is&lt;br&gt;
&amp;quot;COMMONSWITCH=-k -b $LTEMP/cookies -c $LTEMP/cookies.&amp;quot;&lt;br&gt;
&lt;br&gt;
Usually, with secure sites, you need to first hit the site, simply to get session cookies, then hit the logon page, feeding it the correct user/pass, then hit the upload page, feeding it whatever it wants to send a file.&lt;br&gt;
&lt;br&gt;
Note -- you have two switches to send parameters, -d and -F, and they can&apos;t be used in the same connection. -d is for URL encoded parameters, -F is for mulitpart/form-data submissions. Watching the headers as you submit manually will tell you what you need to send, and how you need to send it.&lt;br&gt;
&lt;br&gt;
The command lines can get very long -- make sure you understand how your system handles wrapped lines.&lt;br&gt;
&lt;br&gt;
Curl&apos;s authors provide  &lt;a href=&quot;http://curl.haxx.se/docs/httpscripting.html&quot;&gt;The Art Of Scripting HTTP Requests Using Curl&lt;/a&gt;, which is a big help. &lt;br&gt;
&lt;br&gt;
Finally, realize it may not work. If the site is using things like javascript to generate on-the-fly variables, you&apos;ll need to fake that somehow. If you can&apos;t, curl wont work -- curl isn&apos;t a browser, it just sends HTTP commands and saves the output. This isn&apos;t to say curl isn&apos;t useful (lordy, my life would suck without it) but it does has limits.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312714</guid>
  	<pubDate>Fri, 20 May 2005 06:53:29 -0800</pubDate>
  	<dc:creator>eriko</dc:creator>
</item>
<item>
  	<title>By: ph00dz</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#312737</link>	
  	<description>Can you use PHP to control curl?&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://coolgov.com/etc/curlsample.phps&quot;&gt;Here&apos;s how I do it&lt;/a&gt;, but I&apos;m sure that other, more clever folks can provide alternate implementations...</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-312737</guid>
  	<pubDate>Fri, 20 May 2005 07:58:50 -0800</pubDate>
  	<dc:creator>ph00dz</dc:creator>
</item>
<item>
  	<title>By: miniape</title>
  	<link>http://ask.metafilter.com/18923/How-do-you-handle-authentication-via-cookie-with-CURL#313225</link>	
  	<description>Thanks for all the help. I was able to get half of my job done with the &amp;quot;dump a cookie in your folder and tell curl where it is techinique.&amp;quot; I found a work around for the other half. Cheers.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.18923-313225</guid>
  	<pubDate>Sat, 21 May 2005 04:35:25 -0800</pubDate>
  	<dc:creator>miniape</dc:creator>
</item>

    </channel>
</rss>
