PHP发布到WebAPI [英] PHP post to WebAPI

查看:268
本文介绍了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('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,

推荐答案

在Maalls对此帖子的回答之后

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆