我如何使用 BlackBerry Native Library 在 C++ 中复制这样的函数?
$username="admin";
$password="admin";
$url="http://www.yourdomain.com/";
$cookie="cookie.txt";
$postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In&redirect_to=".
$url ."wp-admin/&testcookie=1";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "wp-login.php");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-admin/");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
exit;
BlackBerry Native Library 中的请求通常如下所示:
QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);
const QString queryUri = QString::fromLatin1("http://website.com/get.php?email=%1").arg(email);
QNetworkRequest request(queryUri);
QNetworkReply* reply = networkAccessManager->get(request);
任何信息或正确方向的一点都会很棒!
最佳答案
libcurl在 BB10 上可用,并且它已经在 sdk 中。不要忘记将 LIBS += -lcurl
添加到您的 .pro
文件
有很多 examples在他们的网站上。节省一些options应该可以很好地从您的 PHP 代码中翻译出来,它应该看起来像 that .
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
几个项目:
- Curl 需要原始数据。
QString
s 隐藏了所有底层的东西。将数据转换为char*
时会很痛苦。使用国际数据进行测试。 - 这是 C,做你的功课,了解什么是线程友好的,什么不是,了解谁负责清理东西。
QNetworkAccessManager
和他的 friend 使用完全不同的异步 API。预计在使 libcurl 工作时会有更多的痛苦。不过也有一些收获:- 这些 API 实现了一个队列,因此您可以启动无限(例如,数千)个任务,Qt/BB10 会限制请求,这样您就不会失去连接,按主机分组请求以节省一些开销,...<
- 这些 API 是移动感知的:想要在 3G 或更好的情况下传输,而不是在手机上传输?他们可以做到。
- 这些是 native Qt API。使用 libcurl 一段时间,您就会明白。
关于c++ - 在 C++ 中使用 PHP cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18668302/