php调用java webapi,PHP发布到WebAPI

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.

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.

Someone gave me then Code :

$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.

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,

解决方案

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);

Thanks Maalls and dbau for the answer :).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值