Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using curl and its getting test data fine but i am unable to send the data from it to my php script
c++ code i am using is taken from curl post examples which is:
C++
CURL * curl;
	curl_global_init(CURL_GLOBAL_ALL);
	CURLcode res;
	string data1 = "testData";
	curl = curl_easy_init();
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL, "addess of my wamp server here with php file");
		//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data1.c_str());
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"name=daniel&project=curl");

		res = curl_easy_perform(curl);
		if (res != CURLE_OK)
		{
			fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
		}
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();


What I have tried:

and this is a very simple php script just for checking if data is coming which is not
PHP
<?php
  global $conn; // db connection variable
  $db_server = "localhost";
  $username = "root";
  $password = "";
  $db_name = "pcinfo";
  // create a connection
  $conn = new mysqli($db_server, $username, $password, $db_name);
  // check connection for errors
  if ($conn->connect_error) {
      die("Error: " . $conn->connect_error);
  }
  else
 // echo"its still working";
  // uncomment the line below to confirm a connection is established
  // echo '<h1 style="color: green;">Connected to DB!</h1>';
  // your can clear these comments afterwards
  $name=mysqli_escape_string($_POST['name']);
  $project=mysqli_escape_string($_POST['project']);

  $data1 = mysqli_escape_string($_POST['data1']);
  if ($data1 == NULL)
	{
		echo "Error, datas is empty! ";	
  }
  if ($name== NULL)
	{
		echo "Error, name is empty! ";	
  }  if ($project == NULL)
	{
		echo "Error, project is empty! ";	
  }
  $sqlInsert = "INSERT INTO test (testdata,name2,project)VALUES ($data1,$name,$project)";

  if ($conn->query($sqlInsert) === TRUE) {
    echo "New record created successfully";
  }
  else
  echo "Unable to store in db!!!";

?>
Posted
Updated 20-Feb-20 21:40pm
v2

got it working with these changes
Php changes:-
$data1=mysqli_escape_string($conn,$_POST['data1']);
  $name=mysqli_escape_string($conn,$_POST['name']);
  $project=mysqli_escape_string($conn,$_POST['project']);

C++ code changes:-
string data1 = "data1="+name+"&name="+n+"&project="+k;

curl_easy_setopt(curl, CURLOPT_POSTFIELDS,data1.c_str());
 
Share this answer
 
I would test the C++ code using a free HTTP server available online. Communication testing is a better experience when one of the endpoints is rock solid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900