libcurl: post data and then get new page data

Hi all:

I'm trying to interact with a web page with my program.
The situation is:

There are two pages: page1.php and page2.php
In page1.php, there are some codes for a form containing a few text fields and three selections. The text and selected options are going to be posted(method=POST) to page2.php
page2.php display what my program selected.

Okay, I've successfully hook up with page1.php
below is the code I have for the moment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  CURL *curl;
	CURLcode res;

	curl = curl_easy_init();
	if(curl)
	{
		/* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
		curl_easy_setopt(curl, CURLOPT_URL, "http://www.mysite.com/page1.php");
		/* Now specify the POST data */
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=h9uest&email=h9uest@h9uest.com&comments=hi");

		/* Perform the request, res will get the return code */
		res = curl_easy_perform(curl);

		/* always cleanup */
		curl_easy_cleanup(curl);
	}
	else
	{
		cerr << "Fail to create curl handle for post method\n";
		exit(1);
	};


Questions:
1, curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=h9uest&email=h9uest@h9uest.com&comments=hi");doesn't deal with selections. How do I post selection data? assume name is "mychoice".

2, how can I get the response, i.e. page2.php? I want to get the display of page2.php and verify that data did get posted.
for you information, page2.php is written as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

 echo($_POST['name'] . "\n");

 echo($_POST['email'] . "\n");
 
 echo($_POST['comments'] . "\n");
 
 $choices = $_POST['mychoice'];
  if(empty($choices))
  {
    echo("You didn't select any items.");
  }
  else
  {
    $N = count($choices);
 
    echo("You selected $N entrie(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($choices[$i+1] . "\n");
    }
  }

 ?>


3, how do I download a jpg file in my program? I've already extracted the url of the image I want.


Thank you very much, and I am looking forward to hearing from you!

Hi All:

I've solved all three problems. In case anyone is interested:

1, A work around:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=h9uest&email=h9uest@h9uest.com&comments=hi&mychoice[1]=choice1&mychoice[2]=choice2&mychoice[3]=choice3");

It's actually quite silly. I used to think there was another way to post a bunch of data in a better way. We just need to use string operations to cascade the ultimate super long string we want to post.

If someone knows a smarter way to do it, please point it out!


2, I got it wrong. I should have posted the data to page2.php, and will get the feedback immediately. Again, a silly mistake of mine.


3, This is not a silly question, though. I could save the image to my hard disk, but when I tried to open the image file, it says "corrupted file" or something. In fact, when we download image files using libcurl, the image file was downloaded as binary. Be cautious, as that implies when we save the downloaded file, we need to save it as binary. In the case of fopen_s, you need to specify "wb", not just "w".


Hope it helps. And if there is any mistake or a better way to do things, please do point it out!
Thanks.
Topic archived. No new replies allowed.