php调用restful接口_php使用yaf编写restful接口

https://www.laruence.com/manual/yaf.routes.custom.html

yaf的框架介绍:

https://www.laruence.com/manual/

restful api介绍:

https://www.ruanyifeng.com/blog/2014/05/restful_api.html

本文起点定在使用yaf搭建好hello world页面。

对于restful api的接口,有两种方式实现,

第一种是自定义路由[https://www.laruence.com/manual/yaf.routes.custom.html]:

示例如下:

class Helper_Router extends Yaf_Request_Abstract implements Yaf_Route_Interface{

public function route($request ){

}

public function assemble (array $mvc, array $query = NULL){

}

}

然后Bootstrp.php加载这个路由即可

第二种使用插件机制,Bootstap加载路由插件

例如api接口:

PUT /zoos/ID

对于http接口的请求可以使用Yaf_Request_Abstract [https://www.laruence.com/manual/yaf.class.request.html]

对应自定义路由中的route函数,使用

$request->getMethod(); 获取 HTTP方法  这里是 “POST"

$request->getRequestUri(); 获取链接 这里是 "/zoos/ID"

通过固定规则的然后使用

$request->setModuleName(...);

$request->setControllerName(...);

$request->setActionName(...);

即可调用对应的规则

示例代码:

支持POST/DELETE/GET/PUT

/zoos/animals/:id?offset=:offset&limit=:limit  类似

public function route($request ){

$method = $request->getMethod();

$uri = $request->getRequestUri();

//首先验证method是否合法

$check_method = strtolower($method);

$allow_method = array('get', 'post', 'put', 'delete');

if(! in_array($check_method, $allow_method)){

throw new Yaf_Exception("{$method} not supported", 405);

}

$uri = ltrim($uri, '/');

$uri_cut = explode('/', $uri);

if(count($uri_cut) < 2){

throw new Yaf_Exception('not exits method', 400);

}

if(count($uri_cut) == 2){

$param = 0;

list($module, $control) = $uri_cut;

}else{

list($module, $control, $param) = $uri_cut;

}

$controller = ucfirst(strtolower($control)) . "_" . ucfirst(strtolower($method));

$request->setModuleName(strtoupper($module));

$request->setControllerName($controller);

$request->setActionName('index');

$request->setParam("id", htmlspecialchars($param, ENT_QUOTES));

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用wxWidgets调用RESTful接口可以通过以下步骤实现: 1. 在代码中引入curl库,可以使用以下代码: ``` #include <curl/curl.h> ``` 2. 创建一个CurlHandle类,用于处理curl库的初始化和请求。以下是一个简单的CurlHandle类的示例代码: ``` class CurlHandle { public: CurlHandle() { curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); } ~CurlHandle() { curl_easy_cleanup(curl); curl_global_cleanup(); } CURL* get() { return curl; } private: CURL* curl; }; ``` 3. 创建一个HTTPRequest类,用于封装http请求。以下是一个简单的HTTPRequest类的示例代码: ``` class HTTPRequest { public: HTTPRequest(const std::string& url) { curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str()); } void setHeader(const std::string& header) { headers = curl_slist_append(headers, header.c_str()); curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers); } void setMethod(const std::string& method) { curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, method.c_str()); } void setBody(const std::string& body) { curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, body.c_str()); } CURLcode perform() { return curl_easy_perform(curl.get()); } private: CurlHandle curl; struct curl_slist* headers = nullptr; }; ``` 4. 在wxWidgets的GUI代码中创建一个按钮,并为其添加一个事件处理程序。在事件处理程序中,创建一个HTTPRequest对象,并设置请求的URL、header、method和body,并执行请求。以下是一个简单的事件处理程序的示例代码: ``` void MyFrame::OnButtonClicked(wxCommandEvent& event) { HTTPRequest request("http://localhost:8080/test"); request.setHeader("Content-Type: application/json"); request.setMethod("POST"); request.setBody("{\"name\": \"test\"}"); CURLcode res = request.perform(); if (res != CURLE_OK) { wxMessageBox("Error: " + std::string(curl_easy_strerror(res))); } } ``` 通过以上步骤,您就可以使用wxWidgets调用RESTful接口了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值