PHP发布到WebAPI [英] PHP post to 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('http://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,
推荐答案
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 = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!