Curl and forms
Posted: Sat Dec 17, 2005 12:26 am
Jcart | Please use
Jcart | Please use
Code: Select all
andCode: 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
andCode: 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]