好吧,我知道标题看起来很简单,但是我从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
发布评论