2013-02-14 139 views
0

我有一个php脚本运行在web服务器上,以便执行一些插入到数据库中。该脚本接收一些加密的数据,解密并将其推入数据库。发送数据从C++程序到php web服务器

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

我在想一些打开URL(http://myserver.com/myscript.php?message=adfafdadfasfasdfasdf)的bash脚本,并通过参数接收消息。

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

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

谢谢!

+0

考虑到约束条件,您的解决方案似乎是合理的。 – mkaatman 2013-02-14 17:00:07

回答

3

一个更强大的解决方案是使用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; 
} 
1

既然你不需要解析HTTP查询的结果,你可以只使用system调用标准工具一样wget

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

这与您正在考虑的基本相同,保存脚本间接。

+0

感谢您的答案!最后,我决定使用libcurl,非常容易使用我的目的:D – user1370912 2013-02-14 20:13:10

相关问题