C语言代码:
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
// 设置POST参数
const char *post_data = "Info=aaaaaa&SomethingElse=bbbbbb";
char url[] = "http://127.0.0.1/test.php?ParamA=testA&ParamB=3.1415926";
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
printf("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
在Ubuntu下安装curl即可使用,具体看另一篇文章:
服务端PHP代码:
<?php
// 获取URL参数
$paramA=$_GET['ParamA'];
$paramB=$_GET['ParamB'];
$info=$_POST['Info'];
$somethingelse=$_POST['SomethingElse'];
// 连接到MySQL数据库
$servername = "127.0.0.1";
$username = "xxxxx";
$password = "xxxxx";
$dbname = "db_xxxxx";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败:" . $conn->connect_error);
}
// 构建SQL插入语句
$sql = "INSERT INTO tb_xxxxx (FieldA, FieldB, FieldC, FieldD) VALUES ('$paramA', '$paramB', '$info', '$somethingelse')";
//echo $sql;
// 执行SQL插入语句
if ($conn->query($sql) === TRUE) {
echo "success";
} else {
echo "fail:" . $sql . "<br>" . $conn->error;
}
// 关闭数据库连接
$conn->close();
?>