cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Re: simple form filler problem

From: Richard Lynch <ceo_at_l-i-e.com>
Date: Mon, 13 Aug 2007 13:41:14 -0500 (CDT)

On Sun, August 12, 2007 9:50 am, Dave Fobare wrote:
> Under WAMP 1.7.2 on XP Pro, I'm trying to get the following simple
> form
> filler to work:
>
> $post = "UserID=myuserid&Password=mypassword";
> $ch = curl_init("http://www.mysite.com/privatelogin");
>
> curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
> $out = curl_exec($ch);
> curl_close($ch);
> echo $out;
>
> All I'm getting out is the login page itself. I tried it another way:
>
> $curl = curl_init();
> curl_setopt($curl,
> CURLOPT_URL,"http://www.mysite.com/privatelogin");
> curl_setopt($curl, CURLOPT_POST, 1);
> curl_setopt($curl, CURLOPT_POSTFIELDS,
> "UserID=myuserid&Password=mypassword");

While later versions allowed an array for POSTFIELDS, it was buggy for
a couple versions, I think, and you might be best off just using the
string as you have been doing...

> curl_exec ($curl);
> curl_close ($curl);
>
> and I get the same result. cURL is working; I've enabled it properly
> in
> the php.ini under Apache2, and php_curl does show up as enabled on the
> WAMP control panel. And I can run other cURL snippets. Those examples
> above are directly cribbed from public sources, but I still must be
> doing something wrong. Help!

It's possible that the login FORM page also has some kind of Cookie to
start a session. (E.g., <?php session_start();?>)

You will need to use CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR to
get/send all the cookies back and forth.

Actually, as I recall, trying to do that with CURLOPT_RETURNTRANSFER
didn't work, so you have to parse the header fields and pull out the
Cookies, and then send them back as Header data in the request.

Here's a couple functions I've used in the past:

  $cookie_jar = array();

  function collect_cookies($curl, $http){
    //$http is the whole response, headers and all
    global $cookie_jar;
    $http = explode("\n", $http);
    $body = false;
    while (!$body && list(, $line) = each($http)){
      #echo strlen($line), ": $line\n";
      if ($line == "\r" || $line == "\n" || $line == "\r\n"){
        $body = true;
      }
      if (stristr($line, 'Set-Cookie:')){
        //Strip of 'Set-Cookie:' in any possible variant:
        $parts = explode(':', $line);
        unset($parts[0]);
        $cookie = trim(implode(':', $parts));
        #echo "Cookie: $cookie\n";
        $parts = explode(';', $cookie);
        list($cookie, $path) = $parts;
        #echo "Cookie: $cookie\n";
        list($key, $value) = explode('=', $cookie);
        $cookie_jar[$key] = $value;
      }
    }
    #echo "CURRENT COOKIES:\n";
    #var_dump($cookie_jar);
    send_cookies($curl);
  }

  //Used to send old cookies when we have a NEW curl connection.
  function send_cookies($curl){
    global $cookie_jar;
    reset($cookie_jar);
    $cookie_string = '';
    while (list($key, $value) = each($cookie_jar)){
      $cookie_string .= "$key=$value;";
    }
    $cookie_string = substr($cookie_string, 0, -1);
    curl_setopt($curl, CURLOPT_COOKIE, $cookie_string);
  }

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2007-08-13