简体   繁体   English

将数据从C ++程序发送到php Web服务器

[英]Send data from c++ program to php web server

I have a php script running in a web server in order to perform some inserts into a database. 我在网络服务器上运行着一个php脚本,以便对数据库执行一些插入操作。 This script receives some encrypted data, decrypts it and push it into the db. 该脚本接收一些加密的数据,将其解密并将其推入数据库。

The responsible to send this data is a C++ program (Running in Linux), this program will send a message of no more than 40 characters every 5 seconds. 负责发送此数据的是一个C ++程序(在Linux中运行),该程序每5秒发送一次不超过40个字符的消息。

I was thinking on call some bash script that opens the URL ( http://myserver.com/myscript.php?message=adfafdadfasfasdfasdf ) and receives the message by argument. 我在考虑调用一些bash脚本来打开URL( http://myserver.com/myscript.php?message=adfafdadfasfasdfasdf )并通过参数接收消息。

I dont want a complex solution, because I just need to open the URL, it is a unidirectional communication channel. 我不需要复杂的解决方案,因为我只需要打开URL,它是一个单向通信通道。

Some simple solution to do this? 一些简单的解决方案可以做到这一点?

Thanks! 谢谢!

A more robust solution would be to use libcurl , which lets you open a http connection in a few lines . 一个更可靠的解决方案是使用libcurl ,它使您可以在几行中打开http连接 Here's the self contained example from the link: 这是链接中包含的示例:

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

Since you don't need to parse the result of the HTTP query, you could just use system to call a standard utility like wget . 由于您不需要解析HTTP查询的结果,因此您可以使用system调用标准实用程序,如wget

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

This is basically the same as what you were thinking about, save the script indirection. 基本上与您所想的相同,只是间接保存了脚本。

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

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