PHP发布到WebAPI

编程入门 行业动态 更新时间:2024-10-26 14:27:27
本文介绍了PHP发布到WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,我知道标题看起来很简单,但是我从3天开始就如何向webapi发出POST请求的示例.

Well, I know that the headline look simple, but i was looking from 3 days for an example on how to make the POST request to webapi.

当前我正在使用JQuery来执行我的POST,但是我需要一些php脚本来运行并与我的C# webAPI对话,并且似乎无法找到一些示例或说明如何执行此操作.

Currently I am using JQuery to do my POST, but I need some php script to run and talk to my C# webAPI, and it seems impossible to find some examples or explain on how to do that.

然后有人给我代码:

$response = file_get_contents('localhost:59040/api/Email/SendEmails'); $response = json_decode($response); echo ($response);

但是,这无能为力-甚至在解决问题的方法上也没有错误.

But this one does nothing - Not even an error on how to go more into the problem.

我简单地需要一个php脚本向获取1个param(String)的webapi发出POST请求,并返回一个正确的答案或错误,

I simpley need a php script to make the POST request to webapi who gets 1 param(String) and return An ok answer or Error,

推荐答案

在Maalls对此帖子的回答之后如何使用PHP发送POST请求?

After Maalls answer from this post How do I send a POST request with PHP?

答案非常简单,代码如下:

The answer was really simple and the code was the following :

$url = 'server/path'; $data = array('key1' => 'value1', 'key2' => 'value2'); // use key 'http' even if you send the request to ... $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); var_dump($result);

感谢Maalls和dbau的回答:).

Thanks Maalls and dbau for the answer :).

更多推荐

PHP发布到WebAPI

本文发布于:2023-10-12 01:16:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   WebAPI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!