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

curl-and-php

Re: Help with Encoding?

From: Brian Wilkins <brian_at_hcc.net>
Date: Mon, 11 Oct 2004 15:17:21 +0600

I don't do POSTing like that. Here is how I do it:

-- snip --

sub connect_via_dispatch {

   my $curl = Curl::easy::init();

   if(!$curl) {
      die "curl init failed!\n";
   }

   my $url = "https://www.url.cc/cgi-bin/script.cgi";
   my $rawHTML = ""; # Stores the HTML returned from curl
   $::errbuf = "";
   Curl::easy::setopt($curl, CURLOPT_ERRORBUFFER, "::errbuf");

   Curl::easy::setopt($curl, CURLOPT_URL, $url);
   Curl::easy::setopt($curl, CURLOPT_NOPROGRESS, 1);
   Curl::easy::setopt($curl, CURLOPT_TIMEOUT, 30);
   Curl::easy::setopt($curl, CURLOPT_HEADERFUNCTION, \&header_callb);
   Curl::easy::setopt($curl, CURLOPT_WRITEFUNCTION, \&body_callb);
   Curl::easy::setopt($curl, CURLOPT_POST, 1);
   Curl::easy::setopt($curl,
CURLOPT_POSTFIELDS,"Service=CDR&PIN=$DC&StartDate=$prev_m-$prev_d-$prev_y&EndDate=$endmonth-$endday-$endyear&PageItems=99999999&Offset=-6");
   Curl::easy::setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   Curl::easy::setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
   Curl::easy::setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0");
   Curl::easy::perform($curl);
   Curl::easy::cleanup($curl);
}

sub body_callb {
   my($chunk,$handle)=@_;
   ${handle} .= $chunk;
   $rawHTML .= $chunk;
# print $rawHTML;
   return length($chunk);
}

sub header_callb {
   return length($_[0]);
}

-- snap --

> Hi,
>
> may be a dumb subject title as im not certain what the issue is but I
> will describe it and provide a little sample code so you can see how
> im doing it. My thanks in advance for any help!
>
> Im trying to (and succeeding) grab a page, and then POST to that
> page. My problem appears to be with the user_agent and referer strings.
>
> If I *do not* urlencode() my UA string and Referer string the page
> can be grabbed but not posted to. However, when *i do* urlencode() these
> strings i get a garbled UA/REFERER in the log files.
>
> How should I handle this? - Do I have another issue that may be causing
> this? - Here's the POST bit of the function im using (it DOES POST
> but the strings are useless...)
>
> case "put":
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_URL, trim($url));
> curl_setopt($ch, CURLOPT_FAILONERROR, 1);
> curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
> curl_setopt($ch, CURLOPT_REFERER, urlencode($_referer));
> curl_setopt($ch, CURLOPT_USERAGENT, urlencode($_userAgent));
> curl_setopt($ch, CURLOPT_POST, 1);
> curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
> curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, CONECTIONTIMEOUT);
> curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
> curl_setopt($ch, CURLOPT_PROXYUSERPWD, PROXYPASS);
> curl_setopt($ch, CURLOPT_PROXY, $proxy2use);
>
> $postfields = array();
> $postfields[] = array("var1", $var1);
> $postfields[] = array("var2", $var2);
>
> foreach($postfields as $subarray) {
> list($foo, $bar) = $subarray;
> $bar = urlencode($bar);
> $postedfields[] = "$foo=$bar";
> }
> $urlstring = join("\n", $postedfields);
> $urlstring = ereg_replace("\n", "&", $urlstring);
>
> curl_setopt($ch, CURLOPT_POSTFIELDS,$urlstring);
>
> $result=curl_exec($ch);
> $error=curl_errno($ch);
>
> curl_close($ch);
> return array($result, $error);
> break;
>
> My thanks again for *any* insight on what i may (am) doing wrong ;-)
> --
> Nick W

--
Brian Wilkins
brian_at_hcc.net
Software Engineer
Heritage Communications Corporation
  Melbourne, FL     USA     32935
Received on 2004-10-11