【问题标题】:Send data from c++ program to php web server将数据从 c++ 程序发送到 php web 服务器
【发布时间】:2013-01-30 13:31:39
【问题描述】:

我有一个在 Web 服务器中运行的 php 脚本,以便对数据库执行一些插入操作。该脚本接收一些加密数据,对其进行解密并将其推送到数据库中。

负责发送此数据的是一个 C++ 程序(在 Linux 中运行),该程序将每 5 秒发送一条不超过 40 个字符的消息。

我正在考虑调用一些 bash 脚本来打开 URL (http://myserver.com/myscript.php?message=adfafdadfasfasdfasdf) 并通过参数接收消息。

我不想要复杂的解决方案,因为我只需要打开网址,它是一个单向的沟通渠道。

一些简单的解决方案来做到这一点?

谢谢!

【问题讨论】:

  • 考虑到限制,您的解决方案似乎是合理的。

标签: php c++ linux bash sockets


【解决方案1】:

更强大的解决方案是使用libcurl,它可以让您open a http connection in a few lines。这是链接中的自包含示例:

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

【讨论】:

    【解决方案2】:

    由于您不需要解析 HTTP 查询的结果,您可以使用 system 来调用像 wget 这样的标准实用程序。

    int retVal = system("wget -O- -q http://whatever.com/foo/bar");
    // handle return value as per the system man page
    

    这和你想的基本一样,省掉脚本间接。

    【讨论】:

    • 感谢您的回答!!最后我决定使用 libcurl,它非常容易用于我的目的:D
    猜你喜欢
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 2015-02-15
    • 2019-12-15
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多