Curl and forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kireol
Forum Newbie
Posts: 3
Joined: Sun Oct 02, 2005 10:11 pm

Curl and forms

Post by kireol »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I'm trying to use the USPS zip code finder.  You give it an address, it gives you the zip code.  It works fine in firefox.  So I was trying to use PHPs curl to send the form variables.  In FireFox I checked the form variables values and made sure I had them all.  There are some I didnt put in because those had null values.

Why does this work fine in a real browser but not in PHP/curl?

Code: Select all

<?PHP
        $cookie_file_path = "cookies/cookiejar.txt"; // Please set your Cookie File path
        $fp = fopen("$cookie_file_path","w") or die("<BR><B>Unable to open cookie file $mycookiefile for write!<BR>");
        fclose($fp);

        //load form page.
        $ch = curl_init();
        $url = "http://zip4.usps.com/zip4/welcome.jsp";
        curl_setopt($ch, CURLOPT_URL, $url);
        $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        $result = curl_exec ($ch);
        print $result;

        //load page result page.

        $url = "http://zip4.usps.com/zip4/zcl_0_results.jsp";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        $mypostfields = urlencode("visited=1&pagenumber=0&address2=1500 PENNSYLVANIA AVE&city=WASHINGTON&state=DC");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $mypostfields);
        $result = curl_exec ($ch);

        print $result;

        curl_close($ch);
?>

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

I'm not too familiar with cURL, but I'll take a stab at the problem. When you urlencode the post fields you're essentially escaping everything, which tells the webserver that there aren't any post variable other than visited. So it escapes the &s and =s so that all the other variables are clumped under the one visited variable.

Try this line:

Code: Select all

mypostfields = "visted=1&pagenumber=0&address2=" . urlencode("1500 PENNSYLVANIA AVE") . "&city=" . urlencode("WASHINGTON") . "&state=" . urlencode("DC");
If visited and pagenumber would be variables, you might have to urlencode the values only, not the variables, &s, and =s...
Post Reply