使用php发送json帖子 [英] Send json post using php
问题描述
我有这个 json 数据:
I have this json data:
{
userID: 'a7664093-502e-4d2b-bf30-25a2b26d6021',
itemKind: 0,
value: 1,
description: 'Saude',
itemID: '03e76d0a-8bab-11e0-8250-000c29b481aa'
}
我需要发布到 json url:http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount
and I need to post into json url: http://domain/OnLeagueRest/resources/onleague/Account/CreditAccount
使用 php 如何发送此帖子请求?
using php how can I send this post request?
推荐答案
无需使用任何外部依赖项或库:
Without using any external dependency or library:
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'header'=> "Content-Type: application/json
" .
"Accept: application/json
"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
$response 是一个对象.可以像往常一样访问属性,例如$response->...
$response is an object. Properties can be accessed as usual, e.g. $response->...
其中 $data 是包含您的数据的数组:
where $data is the array contaning your data:
$data = array(
'userID' => 'a7664093-502e-4d2b-bf30-25a2b26d6021',
'itemKind' => 0,
'value' => 1,
'description' => 'Boa saudaÁ„o.',
'itemID' => '03e76d0a-8bab-11e0-8250-000c29b481aa'
);
警告:如果在 php.ini 中将 allow_url_fopen 设置设为 Off,这将不起作用.
Warning: this won't work if the allow_url_fopen setting is set to Off in the php.ini.
如果您正在为 WordPress 开发,请考虑使用提供的 API:https://developer.wordpress.org/plugins/http-api/
If you're developing for WordPress, consider using the provided APIs: https://developer.wordpress.org/plugins/http-api/
这篇关于使用php发送json帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!