gpt4 book ai didi

php - 如何在 C++ 服务器中解码 curl url 编码的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:02 28 4
gpt4 key购买 nike

我正在使用 C++ pastiche rest api 库在 ubuntu linux 中制作一个 rest api。我已经让服务器正常工作。我可以使用 php curl 将数据发布到我的服务器。服务器接收数据并可以返回数据。问题是这样的。当我使用 curl post 发布到服务器时,它以这样的 url 编码字符串将其发送到服务器 name=percy&age=34&eye_color=blue。我需要知道如何在 C++ 中将每一个都放入一个字符串中。此外,其中一个字段也可能具有二进制数据以及普通字符串。我已经编写了解释二进制数据的代码,但我现在不知道如何从 curl post 转换字符串。请忽略我的端口在我的 php.ini 中不同的事实。原因是我在 virtualbox 中运行 ubuntu。

我需要从我发送的帖子中提取字符串和二进制数据。这是我不知道该怎么做。我不确定我是否需要另一个库来执行此操作

这是我的 php 代码:-

$postData = http_build_query(
array(
'dstdata' => 'hello',
'more' => 'test',
'age' => 34
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'localhost:9999/about');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
print_r($output);
curl_close($ch);

上面的代码会生成这样的字符串 dstdata=hello&more=test&age=34

这里是 C++ 服务器代码。你可以看到我在这里设置了两条路线:-

#include <pistache/router.h>
#include "pistache/endpoint.h"
#include "pistache/http.h"
#include <iostream>

using namespace Pistache;
using namespace Rest;

Rest::Router router;

void sausage(const Rest::Request& request, Http::ResponseWriter response){
std::cout << "We have contact" << std::endl;
response.send(Http::Code::Ok, "Bottoms Up\n");
}

void about(const Rest::Request& request, Http::ResponseWriter response){
std::cout << "Server Running" << std::endl;
response.send(Http::Code::Ok, request.body());
}

int main(){
Routes::Get(router,"/ready",Routes::bind(&sausage));
Routes::Post(router,"/about",Routes::bind(&about));
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(9080));
auto opts = Pistache::Http::Endpoint::options()
.threads(10).flags(
Pistache::Tcp::Options::ReuseAddr);
Http::Endpoint server(addr);
server.init(opts);
server.setHandler(router.handler());
server.serve();
return 0;
}

最佳答案

我现在已经解决了这个问题,但是我不得不改变发送数据的方式。我没有将数据作为表单数据发送,而是像这样以 json 编码发送。

<?php
$data = array("name" => "Paul", "age" => "38");
$data_string = json_encode($data);
$ch = curl_init('localhost:9999/newtest');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);

然后我安装了 nlohmann json 库。我展示的这个示例让我可以将 json 数据发布到我的服务器。我让json解析出来,放到一个句子中返回句子。所以服务器现在正在做我想做的事,我现在可以编写我真正的代码了。因为我使用的是 json 编码,所以我也可以使用它来发送二进制数据。所以看起来我已经解决了我的问题。

#include <pistache/router.h>
#include "pistache/endpoint.h"
#include "pistache/http.h"
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace Pistache;
using namespace Rest;
Rest::Router router;

void newtest(const Rest::Request& request, Http::ResponseWriter response){
std::cout << "NewTest" << std::endl;
auto jj = json::parse(request.body());
std::string name = jj["name"];
std::string age =jj["age"];
std::string test = name+" is my name, and my age is "+age;
response.send(Pistache::Http::Code::Ok, test);
}

int main(){
Routes::Post(router,"/newtest",Routes::bind(&newtest));
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(9080));
auto opts = Pistache::Http::Endpoint::options()
.threads(10).flags(
Pistache::Tcp::Options::ReuseAddr);
Http::Endpoint server(addr);
server.init(opts);
server.setHandler(router.handler());
server.serve();
return 0;
}

关于php - 如何在 C++ 服务器中解码 curl url 编码的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56818691/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com