简体   繁体   English

在C ++中使用PHP cURL

[英]using PHP cURL in C++

How could I replicate such function in C++ using the BlackBerry Native Library? 如何使用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;

A Request in the BlackBerry Native Library would usually look like the following as an example : 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);

Any information or a point in the correct direction would be amazing! 任何信息或正确方向的观点都将是惊人的!

libcurl is available on BB10, and it is already in the sdk. libcurl在BB10上可用,并且已经在sdk中。 Don't forget to add LIBS += -lcurl to your .pro file 不要忘记将LIBS += -lcurl添加到您的.pro文件中

There's a lot of examples on their site. 他们的网站上有很多例子 Save for some options that should translate nicely from your PHP code, it should almost look like that . 除了可以从PHP代码很好地转换的一些选项外 ,它几乎应该像这样

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

A few items: 一些项目:

  • Curl expects raw data. Curl需要原始数据。 QString s hides all the low level stuff. QString s隐藏了所有底层内容。 Expect pain when converting data to char* . 将数据转换为char*时会感到痛苦。 Do test with întèrnâtioñal data. 用întèrnâtioñal数据进行测试。
  • This is C, do your homework and understand what is thread friendly and what is not, understand who is responsible for cleaning stuff. 这是C,做功课,了解什么是线程友好的,什么不是线程友好的,了解谁负责清洁工作。
  • QNetworkAccessManager and his friends use a completely different API that is asynchronous. QNetworkAccessManager和他的朋友使用完全不同的异步API。 Expect incredibly more pain that in making libcurl work. 期望使libcurl发挥更大的痛苦。 There are some gains though: 虽然有一些收获:
    • Those APIs implements a queue, so you can start an unlimited (like, thousands) number of tasks and Qt/BB10 will throttle requests so you don't loose connectivity, group request by hosts to save some overhead, … 这些API实现了一个队列,因此您可以启动无限(如数千个)任务,并且Qt / BB10将限制请求,因此您不会失去连接性,可以通过主机对请求进行分组以节省一些开销,...
    • Those APIs are mobile-aware: want transfer if 3G or better, no transfer on cell? 这些API具有移动感知能力:如果要进行3G或更高级的传输,是否要在单元上进行传输? They can do that. 他们可以做到。
    • These are native Qt API. 这些是本机Qt API。 Work a while with libcurl, you'll understand. 您会了解使用libcurl的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM