如何将使用 CURL 的 PHP 代码转换为 C/C++?
我决定在我的 C++ 服务器中实现 OpenID 登录。我查看了可用的 C++ OpenID 客户端列表,只发现了一个流行的 libopkele
。但对于我的简单服务器来说,它的要求似乎相当高,它有自己的 xml 解析器,使用 boost 进行正则表达式等。
所以我想用C++创建简单的openID客户端,所以我决定移植PHPclient(因为我了解一点PHP)。有一个名为 的类class.openid.php
第一个版本只有 200 行 PHP 代码。我的主要问题是我以前从未在 PHP 或 C++ 中使用过curl。所以我请求帮助将此类 PHP 代码翻译为 C/C++:
function CURL_Request($url, $method="GET", $params = "") { // Remember, SSL MUST BE SUPPORTED
if (is_array($params)) $params = $this->array2url($params);
$curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
if ($method == "POST") curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if (curl_errno($curl) == 0){
$response;
}else{
$this->ErrorStore('OPENID_CURL', curl_error($curl));
}
return $response;
}
So..How to translate such function to C++ (using libCURL) from PHP?
I decided to implement OpenID login into my C++ server. I looked at list of avaliable C++ OpenID clients and found only one popular libopkele
. But its requirements seem quite way 2 much for my simple server which has its own xml parser, uses boost for regexps and so on.
So I want to create simple openID client in C++, so I decided to port PHPclient (because I know PHPa bit). There is class called class.openid.php
which in its first version is just 200 lines of PHP code. My main problem is I never used curl before in PHP nor in c++. SO I beg for help in translating to C/C++ such PHP code:
function CURL_Request($url, $method="GET", $params = "") { // Remember, SSL MUST BE SUPPORTED
if (is_array($params)) $params = $this->array2url($params);
$curl = curl_init($url . ($method == "GET" && $params != "" ? "?" . $params : ""));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPGET, ($method == "GET"));
curl_setopt($curl, CURLOPT_POST, ($method == "POST"));
if ($method == "POST") curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if (curl_errno($curl) == 0){
$response;
}else{
$this->ErrorStore('OPENID_CURL', curl_error($curl));
}
return $response;
}
So.. How to translate such function to C++ (using libCURL) from PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cURL 有一个 C API:链接。您也可以在 C++ 中使用它。
当你使用cURL API时重写你给出的函数应该不难。
cURL has a C API: link. You can use this from C++ too.
It shouldn't be difficult to rewrite the function you gave when you use the cURL API.