kid85 Posted December 31, 2007 Share Posted December 31, 2007 I'm trying to submit form fields to a https site. 1. The site requires to send required fields in POST to be able to access that certain file in that web (sending in GET is not allowed). 2. If those POST fields are correct, then I must manually input a form on that page and click on submit. I'm wondering if the whole process can be done will cURL. I tried sending ALL fields with POST, but it doesn't work. So I guess that the they have to be submitted first with POST and then the rest in GET ? Can this be done ? $ch = curl_init(); // initialize curl handle curl_setopt($ch, CURLOPT_URL, "https://thesite.com/folder/file"); // set url to post to curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s curl_setopt($ch, CURLOPT_POST, 1); // set POST method curl_setopt($ch, CURLOPT_POSTFIELDS, $iputthepostfieldshere); // add POST fields curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($ch, CURLOPT_FTPAPPEND, 1); $result = curl_exec($ch); // run the whole process curl_close($ch); echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/ Share on other sites More sharing options...
MadTechie Posted December 31, 2007 Share Posted December 31, 2007 code looks ok except $iputthepostfieldshere isn't set! which is the post field! Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-426762 Share on other sites More sharing options...
kid85 Posted December 31, 2007 Author Share Posted December 31, 2007 $iputthepostfieldshere is not actually a variable, I just wanted to tell where I place the actual posts. The code is correct but how could I break up $iputthepostfieldshere into POST and GET. I need the first few to be in POST and a few after them to be GET. Can this be done ? Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-426767 Share on other sites More sharing options...
MadTechie Posted December 31, 2007 Share Posted December 31, 2007 Gets go into CURLOPT_URL full example <?php $iputthegetfieldshere = "?test1=one&testtwo=2"; $iputthepostfieldshere = "test3=three&testfour=4"; $ch = curl_init(); // initialize curl handle curl_setopt($ch, CURLOPT_URL, "https://thesite.com/folder/file".$iputthegetfieldshere); // set url to post to curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s curl_setopt($ch, CURLOPT_POST, 1); // set POST method curl_setopt($ch, CURLOPT_POSTFIELDS, $iputthepostfieldshere); // add POST fields curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($ch, CURLOPT_FTPAPPEND, 1); $result = curl_exec($ch); // run the whole process curl_close($ch); echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-426770 Share on other sites More sharing options...
sKunKbad Posted December 31, 2007 Share Posted December 31, 2007 Try removing: curl_setopt($ch, CURLOPT_FTPAPPEND, 1); and curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable and echo $result; Normally if you want to just echo the result like you are doing, returntransfer is not needed because this is the default action of cURL. Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-426778 Share on other sites More sharing options...
kid85 Posted December 31, 2007 Author Share Posted December 31, 2007 None of the 2 suggestions made the code work. I think its because the GET has to somehow be applied after the POST. Any more ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-426968 Share on other sites More sharing options...
sKunKbad Posted January 1, 2008 Share Posted January 1, 2008 I have used cURL to post to different pages in 2 projects in the past. Post fields are added to a cURL post as follows: curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array('name'=>'skunkbad', 'lastname'=>'stinky', 'job'=>'yes', 'age'=>"$age", 'city'=>'Temecula', 'state'=>'California', 'country'=>'USA')); That's all there is to it. If it's not working, then it is something else. Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-427031 Share on other sites More sharing options...
sKunKbad Posted January 1, 2008 Share Posted January 1, 2008 MadTechie is right about appending the GET to the URL in the CURLOPT_URL. In your initial post you said: (sending in GET is not allowed). Quote Link to comment https://forums.phpfreaks.com/topic/83845-curl-help-with-post-get/#findComment-427036 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.