Jump to content

cURL help with POST & GET


kid85

Recommended Posts

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;

Link to comment
Share on other sites

$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 ?

Link to comment
Share on other sites

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;
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.