這篇文章將介紹如何讓 C++ 連線 php 網頁,再透過 php 網頁去取得資料庫的資料。
環境
windows 11 64bit
Visual Studio 2022
遠端主機 cloudways
MariaDB 10.4
php 7.4
準備主程式
#include <iostream>
#include <fstream>
#include <ostream>
#include <cstdlib>
#include <string>
#include <windows.h>
#include <wininet.h>
using namespace std;
#pragma comment(lib,"Wininet.lib")
const int BUF_SIZE = 1024;
const int URL_SIZE = 1024;
void Download01();
void Download02();
void Download03();
int main()
{
//Download01(); //傳遞簡單字串和數字參數並回傳
//Download02(); //取得 MySQL 資料庫資料
//Download03(); //取得 MySQL 資料庫資料,並存在文字檔裡
system("pause");
return 0;
}
1. 連線 php 網頁-傳遞簡單字串和數字參數並回傳
php 程式碼 – cpptest0203.php
<?php
$userName = $_GET["name"];
$id = $_GET["id"];
echo $userName.$userName."#".$id;
?>
C++ 程式碼
void Download01()
{
cout << "測試連線 php 網頁-傳遞簡單字串和數字參數並回傳" << endl;
char szUrl[1024] = "";
char szName[128] = "Harry";
int nID = 1;
char szUrl02[1024] = "http://localhost/cpptest0203.php?name=%s&id=%d";
sprintf_s(szUrl, 1024, szUrl02, szName, nID);
char szBuff[BUF_SIZE] = "";
HINTERNET hUrl, hInet;
DWORD rcv, total = 0;
if (InternetAttemptConnect(0) != ERROR_SUCCESS)
{
cout << "Internet connect failed." << endl;
return;
}
hInet = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
if (hInet == NULL)
{
cout << "Internet open failed." << endl;
return;
}
hUrl = InternetOpenUrlA(hInet, szUrl, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (hUrl == NULL)
{
cout << "Internet open url failed." << endl;
InternetCloseHandle(hInet);
return;
}
do
{
InternetReadFile(hUrl, &szBuff, BUF_SIZE, &rcv);
total += rcv;
} while (rcv > 0);
cout << "receive " << total << " byte." << endl;
szBuff[total] = '\0';
cout << szBuff << endl;
InternetCloseHandle(hUrl);
InternetCloseHandle(hInet);
}
執行結果
2. 連線 php 網頁-取得 MySQL 資料庫資料
建立 MySQL 資料表
-- ----------------------------
-- Table structure for test01
-- ----------------------------
DROP TABLE IF EXISTS `test01`;
CREATE TABLE `test01` (
`id` int(11) NOT NULL,
`int_test` int(11) NOT NULL,
`char_test` char(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`varchar_test` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`text_test` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of test01
-- ----------------------------
INSERT INTO `test01` VALUES (1, 10, 'a', 'Harry Potter', 'The novels chronicle the lives of a young wizard, Harry Potter, whom are students at Hogwarts School of Witchcraft and Wizardry.');
INSERT INTO `test01` VALUES (2, 20, 'b', '哈利波特', '是一部兒童奇幻文學系列小說,描寫主角哈利波特在霍格華茲魔法學校7年學習生活中的冒險故事。');
INSERT INTO `test01` VALUES (3, 30, 'c', 'ハリー・ポッター', '1990年代のイギリスを舞台に、魔法使いの少年ハリー・ポッターの学校生活。');
SET FOREIGN_KEY_CHECKS = 1;
php 程式碼 – cpptest0101.php
<?php
// 建立MySQL的資料庫連接
$host = "localhost";
$dbuser = "user";
$dbpassword = "password";
$dbname = "database";
$link = mysqli_connect($host,$dbuser,$dbpassword,$dbname);
if ( !$link )
{
echo "不正確連接資料庫 " . mysqli_connect_error();
echo "<br>";
exit();
}
mysqli_query($link, "SET NAMES utf8");
mysqli_query($link, "SET CHARACTER_SET_database= utf8");
mysqli_query($link, "SET CHARACTER_SET_CLIENT= utf8");
mysqli_query($link, "SET CHARACTER_SET_RESULTS= utf8");
$sql = "select * from test01";
$result = mysqli_query($link, $sql);
if (!$result)
{
echo "{$sql} 語法執行失敗,錯誤訊息 " . mysqli_error($link);
echo "<br>";
mysqli_close($link); // 關閉資料庫連接
exit();
}
if (mysqli_num_rows($result)<=0)
{
echo "沒有資料";
echo "<br>";
mysqli_close($link); // 關閉資料庫連接
exit();
}
while($array = mysqli_fetch_array($result))
{
echo $array["int_test"]."#";
echo $array["varchar_test"]."#";
}
mysqli_free_result($result);
mysqli_close($link); // 關閉資料庫連接
?>
C++ 程式碼
void Download02()
{
cout << "測試連線 php 網頁-取得 MySQL 資料庫資料" << endl;
char szBuff[BUF_SIZE] = "";
char szUrl[1024] = "http://localhost/cpptest0101.php";
HINTERNET hUrl, hInet;
DWORD rcv, total = 0;
if (InternetAttemptConnect(0) != ERROR_SUCCESS)
{
cout << "Internet connect failed." << endl;
return;
}
hInet = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
if (hInet == NULL)
{
cout << "Internet open failed." << endl;
return;
}
hUrl = InternetOpenUrlA(hInet, szUrl, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (hUrl == NULL)
{
cout << "Internet open url failed." << endl;
InternetCloseHandle(hInet);
return;
}
do
{
InternetReadFile(hUrl, &szBuff, BUF_SIZE, &rcv);
total += rcv;
} while (rcv > 0);
cout << "receive " << total << " byte." << endl;
szBuff[total] = '\0';
cout <<szBuff<< endl; //收到的檔案是 utf-8 字串,直接印出會是亂碼
//把 utf-8 字串轉成 unicode 並印出,hkstr 是我自己寫的字串轉換函式
wchar_t szUni[1024] = L"";
hkstr::UTF8ToUnicode(szUni, szBuff);
hkstr::PrintfUnicode(L"%s\n", szUni);
InternetCloseHandle(hUrl);
InternetCloseHandle(hInet);
}
執行結果
3. 連線 php 網頁-取得 MySQL 資料庫資料並存進文字檔
C++ 程式碼
void Download03()
{
cout << "測試連線 php 網頁-取得 MySQL 資料庫資料,並存在文字檔裡" << endl;
ofstream output("test.txt");
char szBuff[BUF_SIZE] = "";
char szUrl[1024] = "http://localhost/cpptest0101.php";
HINTERNET hUrl, hInet;
DWORD rcv, total = 0;
if (InternetAttemptConnect(0) != ERROR_SUCCESS)
{
cout << "Internet connect failed." << endl;
return;
}
hInet = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
if (hInet == NULL)
{
cout << "Internet open failed." << endl;
return;
}
hUrl = InternetOpenUrlA(hInet, szUrl, 0, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0);
if (hUrl == NULL)
{
cout << "Internet open url failed." << endl;
InternetCloseHandle(hInet);
return;
}
do
{
InternetReadFile(hUrl, &szBuff, BUF_SIZE, &rcv);
output.write(szBuff, rcv);
total += rcv;
} while (rcv > 0);
cout << "receive " << total << " byte." << endl;
szBuff[total] = '\0';
output.close();
InternetCloseHandle(hUrl);
InternetCloseHandle(hInet);
}
執行結果