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

curl-and-php

Help with Encoding?

From: Nick Wilson <nick_at_stylesheet.org>
Date: Mon, 11 Oct 2004 20:17:14 +0200

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
Received on 2004-10-11