如何使用CURL将这样的PHP代码翻译成C / C ++?

时间:2011-07-17 13:37:19

标签: php c++ c openid libcurl

我决定在我的C ++服务器中实现OpenID登录。我查看了可用的C ++ OpenID客户端列表,发现只有一个流行的libopkele。但是对于我的简单服务器来说,它的要求似乎非常方便,它有自己的xml解析器,对正则表达式使用boost等等。

所以我想用C ++创建简单的openID客户端,所以我决定移植PHPclient(因为我知道PHPa位)。有一个名为class.openid.php的类,它的第一个版本只有200行PHP代码。我的主要问题是我以前从未在PHP和c ++中使用curl。所以我请求帮助转换为C / C ++这样的PHP代码:

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;
}

那么..如何将这样的函数从PHP转换为C ++(使用libCURL)?

1 个答案:

答案 0 :(得分:5)

cURL有一个C API:link。你也可以在C ++中使用它。

使用cURL API时,重写您提供的功能应该不难。