mcloan Posted November 28, 2006 Share Posted November 28, 2006 Hello, I am trying to use CURL to post and return results from a form with the below curl function. I have this function working on one site, but can not get it to return results on another site. Are there things a site can do to prevent you from returning results via curl? Below the dotted line are the essential elements of the form that is not working. Can some help with why this may not be working and what I can do to get it to work? After this curl function runs if I use print_r ($_POST); I get an empty array.[code]$data=array('ZipCode' => ‘90277’);// set URL and other appropriate optionscurl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_USERAGENT, 'deepcrawler bot spider/0.1');curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// grab URL, and return output$Contents = curl_exec($ch);// close curl resource, and free up system resourcescurl_close($ch);unset($ch);-------------------------------------------This is the form that is not working------------------------------------<form name="zip_search" method="post" action="dl_frameset.asp"><td><input type="text" id="text1" name="ZipCode" value="" size="5" maxlength="5"></td><a href="#" onClick="validateZip();" onMouseOver="if (blnLoaded) {rollOn(objImgLocate1)}" onMouseOut="if (blnLoaded) {rollOff(objImgLocate1)}"><img src="/images/dealer_locator/button_locate.gif" alt="Locate" name="imageLocate1" width="42" height="17" border="0" id="imageLocate1"></a>[/code]Thank you. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 28, 2006 Share Posted November 28, 2006 I'll ask the obvious questions first.. is Curl installed on the site where it doesn't work?Does the script fail altogether, or do you just get an empty result?Also, according to the documentation, POSTFIELDS should be a string rather than an array. Eg[code=php:0]$data = "ZipCode=90277";[/code] Quote Link to comment Share on other sites More sharing options...
mcloan Posted November 28, 2006 Author Share Posted November 28, 2006 [quote author=btherl link=topic=116526.msg474800#msg474800 date=1164675636]I'll ask the obvious questions first.. is Curl installed on the site where it doesn't work?Does the script fail altogether, or do you just get an empty result? Also, according to the documentation, POSTFIELDS should be a string rather than an array. Eg[code=php:0]$data = "ZipCode=90277";[/code][/quote]I was not aware the site I am calling has to have curl installed. Is it a requirement? If I echo $Contents the script does return the url called via curl it just does not post the results. Regarding the array, on the site that is working I am using $data in an array as the following and it is retruning the results no problemdata=array('zip' => $value, 'distance' =>'500'); You further help is much appreciated.Thank you. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 28, 2006 Share Posted November 28, 2006 Yes, it's a requirement for curl to be installed. But I expect most sites would have it. Since your script doesn't crash, curl must be installed :) If it wasn't, it would fail when trying to call curl_init().I'm not sure what you mean by "the script does return the url called via curl, it just does not post the results". What do you see when you echo $contents ?And about POSTFIELDS as an array or as a string, it may work on the site that it works on, but we are looking at the site where it doesn't work :) It's worth trying the string format instead. Maybe the site where it doesn't work doesn't support the array syntax. For the example you gave there, you would use:[code=php:0]$data = "zip=$value&distance=500";[/code]If you're dealing with strings rather than integers, then you should urlencode() each variable value in the postfields string. Quote Link to comment Share on other sites More sharing options...
mcloan Posted November 28, 2006 Author Share Posted November 28, 2006 [quote author=btherl link=topic=116526.msg474828#msg474828 date=1164677434]Yes, it's a requirement for curl to be installed. But I expect most sites would have it. Since your script doesn't crash, curl must be installed :) If it wasn't, it would fail when trying to call curl_init().I'm not sure what you mean by "the script does return the url called via curl, it just does not post the results". What do you see when you echo $contents ?And about POSTFIELDS as an array or as a string, it may work on the site that it works on, but we are looking at the site where it doesn't work :) It's worth trying the string format instead. Maybe the site where it doesn't work doesn't support the array syntax. For the example you gave there, you would use:[code=php:0]$data = "zip=$value&distance=500";[/code]If you're dealing with strings rather than integers, then you should urlencode() each variable value in the postfields string.[/quote]Good point I will give a try with the string. What I mean is if I echo $contents it opens the page that was specified as $url in the curl statement. But if you post to that page it redirects to another page to show the results in which case this redirect is not happening becasue there are no results showing when I echo $contents. Can I ask you why most sites have curl installed I thought curl was more of a php thing. Is this not the case? Sorry if I am basic I am fairly new to php.Thank you. Quote Link to comment Share on other sites More sharing options...
mcloan Posted November 28, 2006 Author Share Posted November 28, 2006 I tried the string being $data="ZipCode=90277"; and it is still not working. I got the exact same results. If I print_r ($_POST); is just shows Array()and if I print_r ($Contents); it just shows the page of $url without the results retruned from the post. So if I did it manually it just looks like I pulled up the page by typing in the url to a browser, but it did not return the post results when I typed in a zip code and hit submit.Any further guidance you can provide is much appreciated. This one is really killing me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.